0498. 对角线遍历 #15
utterances-bot
started this conversation in
Comments
Replies: 4 comments
-
|
不需要方向管理的,我感觉更简单理解: |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
1 如果在最后一列,则向下方移动,即 x += 1。 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
"其余情况想右上方向移动" 原文中有笔误“想(向)” |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
也可以把对角线数量算出来,再算上每个对角线的起点位置就容易点 class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m = len(mat)
n = len(mat[0])
i=j=0
res = []
for diagonal in range(0, m+n):
if diagonal%2==0:
i = min(diagonal, m-1)
j = diagonal-i
while i >= 0 and j < n:
res.append(mat[i][j])
i -= 1
j += 1
else:
j = min(diagonal, n-1)
i = diagonal -j
while j >= 0 and i < m:
res.append(mat[i][j])
i += 1
j -= 1
return res |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
0498. 对角线遍历
https://algo.itcharge.cn/solutions/0400-0499/diagonal-traverse/
Beta Was this translation helpful? Give feedback.
All reactions