-
Notifications
You must be signed in to change notification settings - Fork 5
/
diagonal_traverse_ii.go
78 lines (59 loc) · 1.28 KB
/
diagonal_traverse_ii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
func findDiagonalOrder(nums [][]int) []int {
var res [][]int
m, size := len(nums), 0
for i := 0; i < m; i++ {
n, x := len(nums[i]), i
for j := 0; j < n; j++ {
if len(res) == x {
res = append(res, []int{})
}
res[x] = append(res[x], nums[i][j])
x++
size++
}
}
ans := make([]int, size)
idx := 0
for i := 0; i < len(res); i++ {
for j := len(res[i]) - 1; j >= 0; j-- {
ans[idx] = res[i][j]
idx++
}
}
return ans
}
/*
func findDiagonalOrder(nums [][]int) []int {
queue := list.New()
queue.PushBack([2]int{0, 0})
numRows := len(nums)
var elems int
for _, lst := range nums {
elems += len(lst)
}
res := make([]int, elems)
resIdx := 0
for queue.Len() > 0 {
size := queue.Len()
for i := 0; i < size; i++ {
next := queue.Remove(queue.Front()).([2]int)
row, col := next[0], next[1]
currentList := nums[row]
if currentList[col] == math.MaxInt32 {
continue
}
res[resIdx] = currentList[col]
resIdx++
currentList[col] = math.MaxInt32
if row+1 < numRows && len(nums[row+1]) > col && nums[row+1][col] != math.MaxInt32 {
queue.PushBack([2]int{row + 1, col})
}
if col+1 < len(currentList) && currentList[col+1] != math.MaxInt32 {
queue.PushBack([2]int{row, col + 1})
}
}
}
return res
}
*/