# 1424. Diagonal Traverse II ###### tags: `Leetcode` `Medium` `FaceBook` `Diagonal` Link: https://leetcode.com/problems/diagonal-traverse-ii/ ## 思路 利用同一条对角线的所有元素的row+col都相等的性质 构建hashmap 存下所有元素 但是由于遍历是从上往下遍历的,所以装进答案的时候需要反着装,因为对角线是从下往上的 ## Code ```java= class Solution { public int[] findDiagonalOrder(List<List<Integer>> nums) { Map<Integer, List<Integer>> map = new HashMap<>(); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; int count = 0; for(int i = 0;i < nums.size();i++){ List<Integer> curr = nums.get(i); int size = curr.size(); for(int j = 0; j < size;j++){ min = Math.min(min, i+j); max = Math.max(max, i+j); count++; if(!map.containsKey(i+j)){ map.put((i+j), new ArrayList<Integer>()); } map.get(i+j).add(curr.get(j)); } } int[] ans = new int[count]; int idx = 0; for(int i = min;i <= max;i++){ List<Integer> curr = map.get(i); int size = curr.size(); for(int j = size-1;j >= 0;j--){ ans[idx++] = curr.get(j); } } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up