鱼C论坛

 找回密码
 立即注册
查看: 2149|回复: 0

[学习笔记] Leetcode 498. Diagonal Traverse

[复制链接]
发表于 2020-8-2 07:19:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 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.



  2. Example:

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

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

  10. Explanation:

  11. Screenshot from 2020-08-01 19-15-38.png

  12. Note:

  13. The total number of elements of the given matrix will not exceed 10,000.
复制代码

  1. class Solution:
  2.     def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
  3.         result = []
  4.         if matrix == None or len(matrix) == 0 or len(matrix[0]) == 0:
  5.             return result
  6.         m = len(matrix)
  7.         n = len(matrix[0])
  8.         hashmap = collections.defaultdict(list)
  9.         for i in range(m):
  10.             for j in range(n):
  11.                 hashmap[i + j].append(matrix[i][j])
  12.         for k in sorted(hashmap.keys()):
  13.             temp = hashmap[k]
  14.             if k % 2 == 0:
  15.                 temp = temp[::-1]
  16.             result += temp
  17.         return result
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 05:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表