-
Notifications
You must be signed in to change notification settings - Fork 0
/
059.go
55 lines (46 loc) · 861 Bytes
/
059.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
package p059
/**
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
*/
func generateMatrix(n int) [][]int {
mat := make([][]int, n)
for i := 0; i < n; i++ {
mat[i] = make([]int, n)
}
rowStart := 0
rowEnd := n - 1
colStart := 0
colEnd := n - 1
fill := 1
for rowStart <= rowEnd && colStart <= colEnd {
for i := colStart; i <= colEnd; i++ {
mat[rowStart][i] = fill
fill++
}
rowStart++
for i := rowStart; i <= rowEnd; i++ {
mat[i][colEnd] = fill
fill++
}
colEnd--
for i := colEnd; i >= colStart; i-- {
mat[rowEnd][i] = fill
fill++
}
rowEnd--
for i := rowEnd; i >= rowStart; i-- {
mat[i][colStart] = fill
fill++
}
colStart++
}
return mat
}