Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75279ea

Browse files
committed
Update 0498. 对角线遍历.md
1 parent bb00113 commit 75279ea

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

‎Solutions/0498. 对角线遍历.md‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,50 @@
55

66
## 题目大意
77

8-
给你一个大小为 `m * n` 的矩阵 `mat`
8+
**描述**:给定一个大小为 `m * n` 的矩阵 `mat`
99

10-
要求:以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
10+
**要求**:以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。
11+
12+
**说明**:
13+
14+
- $m == mat.length$。
15+
- $n == mat[i].length$。
16+
- 1ドル \le m, n \le 10^4$。
17+
- 1ドル \le m * n \le 10^4$。
18+
- $-10^5 \le mat[i][j] \le 10^5$。
19+
20+
**示例**:
21+
22+
```Python
23+
输入:mat = [[1,2,3],[4,5,6],[7,8,9]]
24+
输出:[1,2,4,7,5,3,6,8,9]
25+
```
26+
27+
![](https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg)
1128

1229
## 解题思路
1330

31+
### 思路 1:找规律 + 考虑边界问题
32+
1433
这道题的关键是「找规律」和「考虑边界问题」。
1534

1635
找规律:
1736

18-
- 当行号 + 列号为偶数时,遍历方向为从左下到右上。可以记为右上方向(-1, +1),即行号 -1,列号 +1
19-
- 当行号 + 列号为奇数时,遍历方向为从右上到左下。可以记为左下方向(+1, -1),即行号 +1,列号 -1
37+
1. 当「行号 + 列号」为偶数时,遍历方向为从左下到右上。可以记为右上方向`(-1, +1)`,即行号减 `1`,列号加 `1`
38+
2. 当「行号 + 列号」为奇数时,遍历方向为从右上到左下。可以记为左下方向`(+1, -1)`,即行号加 `1`,列号减 `1`
2039

2140
边界情况:
2241

23-
- 向右上方向移动时:
24-
- 如果在最后一列,则向下方移动,即 `x += 1`
25-
- 如果在第一行,则向右方移动,即 `y += 1`
26-
- 其余情况想右上方向移动,即 `x -= 1``y += 1`
27-
- 向左下方向移动时:
28-
- 如果在最后一行,则向右方移动,即 `y += 1`
29-
- 如果在第一列,则向下方移动,即 `x += 1`
30-
- 其余情况向左下方向移动,即 `x += 1``y -= 1`
42+
1. 向右上方向移动时:
43+
1. 如果在最后一列,则向下方移动,即 `x += 1`
44+
2. 如果在第一行,则向右方移动,即 `y += 1`
45+
3. 其余情况想右上方向移动,即 `x -= 1``y += 1`
46+
2. 向左下方向移动时:
47+
1. 如果在最后一行,则向右方移动,即 `y += 1`
48+
2. 如果在第一列,则向下方移动,即 `x += 1`
49+
3. 其余情况向左下方向移动,即 `x += 1``y -= 1`
3150

32-
##代码
51+
### 思路 1:代码
3352

3453
```Python
3554
class Solution:
@@ -68,6 +87,11 @@ class Solution:
6887
return ans
6988
```
7089

90+
### 思路 1:复杂度分析
91+
92+
- **时间复杂度**:$O(m \times n)$。其中 $m$、$n$ 分别为二维矩阵的行数、列数。
93+
- **空间复杂度**:$O(m * n)$。如果算上答案数组的空间占用,则空间复杂度为 $O(m * n)$。不算上则空间复杂度为 $O(1)$。
94+
7195
## 参考资料
7296

7397
- 【题解】[「498. 对角线遍历」最简单易懂! - 对角线遍历 - 力扣(LeetCode)](https://leetcode.cn/problems/diagonal-traverse/solution/498-dui-jiao-xian-bian-li-zui-jian-dan-y-ibu3/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /