鱼C论坛

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

[学习笔记] Leetcode 1424. Diagonal Traverse II

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

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

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

x
  1. Given a list of lists of integers, nums, return all elements of nums in diagonal order as shown in the below images.


  2. Example 1:

  3. Screenshot from 2020-08-01 19-59-24.png

  4. Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
  5. Output: [1,4,2,7,5,3,8,6,9]
  6. Example 2:

  7. Screenshot from 2020-08-01 19-59-36.png

  8. Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
  9. Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
  10. Example 3:

  11. Input: nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
  12. Output: [1,4,2,5,3,8,6,9,7,10,11]
  13. Example 4:

  14. Input: nums = [[1,2,3,4,5,6]]
  15. Output: [1,2,3,4,5,6]


  16. Constraints:

  17. 1 <= nums.length <= 10^5
  18. 1 <= nums[i].length <= 10^5
  19. 1 <= nums[i][j] <= 10^9
  20. There at most 10^5 elements in nums.
复制代码

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

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 23:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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