Skip to content

Commit ffb06c3

Browse files
committed
leetcode
1 parent 4dee207 commit ffb06c3

File tree

3 files changed

+409
-0
lines changed

3 files changed

+409
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
3+
-- 1424. Diagonal Traverse II--
4+
5+
6+
Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
14+
Output: [1,4,2,7,5,3,8,6,9]
15+
Example 2:
16+
17+
18+
Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
19+
Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
20+
21+
22+
Constraints:
23+
24+
1 <= nums.length <= 105
25+
1 <= nums[i].length <= 105
26+
1 <= sum(nums[i].length) <= 105
27+
1 <= nums[i][j] <= 105
28+
29+
*/
30+
31+
import 'dart:collection';
32+
33+
34+
class Solution {
35+
List<int> findDiagonalOrder(List<List<int>> nums) {
36+
final HashMap<int, List<int>> diagonals = HashMap();
37+
final List<int> ans = <int>[];
38+
final int n = nums.length;
39+
40+
for (int i = 0; i < n; i++) {
41+
final int m = nums[i].length;
42+
for (int j = 0; j < m; j++) {
43+
final int sum = i + j;
44+
if (!diagonals.containsKey(sum)) {
45+
diagonals[sum] = <int>[];
46+
}
47+
diagonals[sum]?.add(nums[i][j]);
48+
}
49+
}
50+
51+
diagonals.forEach((sum, arr) {
52+
for (int i = arr.length - 1; i >= 0; i--) {
53+
ans.add(arr[i]);
54+
}
55+
});
56+
57+
return ans;
58+
}
59+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
func findDiagonalOrder(nums [][]int) []int {
4+
var res [][]int
5+
m, size := len(nums), 0
6+
7+
for i := 0; i < m; i++ {
8+
n, x := len(nums[i]), i
9+
for j := 0; j < n; j++ {
10+
if len(res) == x {
11+
res = append(res, []int{})
12+
}
13+
res[x] = append(res[x], nums[i][j])
14+
x++
15+
size++
16+
}
17+
}
18+
19+
ans := make([]int, size)
20+
idx := 0
21+
22+
for i := 0; i < len(res); i++ {
23+
for j := len(res[i]) - 1; j >= 0; j-- {
24+
ans[idx] = res[i][j]
25+
idx++
26+
}
27+
}
28+
29+
return ans
30+
}
31+
/*
32+
33+
34+
func findDiagonalOrder(nums [][]int) []int {
35+
queue := list.New()
36+
queue.PushBack([2]int{0, 0})
37+
numRows := len(nums)
38+
var elems int
39+
40+
for _, lst := range nums {
41+
elems += len(lst)
42+
}
43+
44+
res := make([]int, elems)
45+
resIdx := 0
46+
47+
for queue.Len() > 0 {
48+
size := queue.Len()
49+
50+
for i := 0; i < size; i++ {
51+
next := queue.Remove(queue.Front()).([2]int)
52+
row, col := next[0], next[1]
53+
54+
currentList := nums[row]
55+
if currentList[col] == math.MaxInt32 {
56+
continue
57+
}
58+
59+
res[resIdx] = currentList[col]
60+
resIdx++
61+
62+
currentList[col] = math.MaxInt32
63+
64+
if row+1 < numRows && len(nums[row+1]) > col && nums[row+1][col] != math.MaxInt32 {
65+
queue.PushBack([2]int{row + 1, col})
66+
}
67+
68+
if col+1 < len(currentList) && currentList[col+1] != math.MaxInt32 {
69+
queue.PushBack([2]int{row, col + 1})
70+
}
71+
}
72+
}
73+
74+
return res
75+
}
76+
77+
78+
*/

0 commit comments

Comments
 (0)