-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspiralOrder.go
More file actions
50 lines (44 loc) · 1.05 KB
/
Copy pathspiralOrder.go
File metadata and controls
50 lines (44 loc) · 1.05 KB
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
/* https://leetcode.com/problems/spiral-matrix/description/
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
*/
package larray
func spiralOrder(matrix [][]int) []int {
if len(matrix) == 0 {
return []int{}
}
m, n := len(matrix), len(matrix[0])
length := m * n
res, count := make([]int, length, length), 0
rowStart, rowEnd, colStart, colEnd := 0, m-1, 0, n-1
for count < length {
for i := colStart; count < length && i <= colEnd; i++ {
res[count] = matrix[rowStart][i]
count++
}
rowStart++
for j := rowStart; count < length && j <= rowEnd; j++ {
res[count] = matrix[j][colEnd]
count++
}
colEnd--
for i := colEnd; count < length && i >= colStart; i-- {
res[count] = matrix[rowEnd][i]
count++
}
rowEnd--
for j := rowEnd; count < length && j >= rowStart; j-- {
res[count] = matrix[j][colStart]
count++
}
colStart++
}
return res
}