Skip to content

Commit

Permalink
对角遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Sep 22, 2018
1 parent b6d32a2 commit 0f7384c
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lc498. Diagonal Traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution:
def findDiagonalOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix:
return matrix
direcs = [(-1, 1), (1, -1)]
d = 0
res = []
pos = [0, 0]
M = len(matrix)
N = len(matrix[0])
size = M * N
for i in range(size):
res.append(matrix[pos[0]][pos[1]])
pos[0] += direcs[d][0]
pos[1] += direcs[d][1]
# 顺序不能变,先判断是否超过右边和下边。也就是==M or ==N,
# 如果先判断是否==-1,那么在角上越界就会出错
if pos[0] == M:
pos[1] += 2
d = 1 - d
pos[0] = M - 1
elif pos[1] == N:
pos[1] = N - 1
d = 1 - d
pos[0] += 2
elif pos[0] == -1:
d = 1 - d
pos[0] = 0
elif pos[1] == -1:
d = 1 - d
pos[1] = 0

return res


nums = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
s = Solution()
print(s.findDiagonalOrder(nums))

1 comment on commit 0f7384c

@Azureki
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

思路参考grandyang

Please sign in to comment.