498. Diagonal Traverse 对角线遍历矩阵

 Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.

Example:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output:  [1,2,4,7,5,3,6,8,9]
Explanation:

Note:

  1. The total number of elements of the given matrix will not exceed 10,000.

 
   
  1. class Solution:
  2. def findDiagonalOrder(self, matrix):
  3. """
  4. :type matrix: List[List[int]]
  5. :rtype: List[int]
  6. """
  7. if not matrix or not matrix[0]:
  8. return []
  9. res = []
  10. isRe = 1
  11. def addLine(row, col, isRe):
  12. line = []
  13. while row < len(matrix) and col >= 0:
  14. line.append(matrix[row][col])
  15. row += 1
  16. col -= 1
  17. l = len(res)
  18. if isRe:
  19. res[l:l] = line[::-1]
  20. else:
  21. res[l:l] = line
  22. for i in range(len(matrix[0])):
  23. addLine(0, i, isRe)
  24. isRe ^= 1
  25. for i in range(1, len(matrix)):
  26. addLine(i, len(matrix[i]) - 1, isRe)
  27. isRe ^= 1
  28. return res






转载于:https://www.cnblogs.com/xiejunzhao/p/8419850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值