|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- Given a list of lists of integers, nums, return all elements of nums in diagonal order as shown in the below images.
-
- Example 1:
-
- Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
- Output: [1,4,2,7,5,3,8,6,9]
- Example 2:
-
- Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
- Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
- Example 3:
- Input: nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
- Output: [1,4,2,5,3,8,6,9,7,10,11]
- Example 4:
- Input: nums = [[1,2,3,4,5,6]]
- Output: [1,2,3,4,5,6]
-
- Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i].length <= 10^5
- 1 <= nums[i][j] <= 10^9
- There at most 10^5 elements in nums.
复制代码
- class Solution:
- def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]:
- result = []
- if nums == None or len(nums) == 0:
- return result
- hashmap = collections.defaultdict(list)
- m = len(nums)
- for i in range(m):
- for j in range(len(nums[i])):
- hashmap[i + j].append(nums[i][j])
- for k in sorted(hashmap.keys()):
- temp = hashmap[k]
- temp = temp[::-1]
- result += temp
- return result
-
复制代码 |
|