-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral-traverse.go
96 lines (78 loc) · 2.42 KB
/
spiral-traverse.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"reflect"
)
/*
Write a function that takes in an n x m two-dimensional array
(that can be square-shaped when n === m) and returns a one-dimensional
array of all the array's elements in spiral order.
Spiral order starts at the top left corner of the two-dimensional array,
goes to the right, and proceeds in a spiral pattern all the way until
every element has been visited.
example:
input: [
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]
]
output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
*/
func spiralTraverse(array [][]int) []int {
if len(array) == 0 {
return []int{}
}
result := []int{}
// Set bounds
startRow, endRow := 0, len(array)-1
startCol, endCol := 0, len(array[startRow])-1
for startRow <= endRow && startCol <= endCol {
for col := startCol; col <= endCol; col++ {
result = append(result, array[startRow][col])
}
for row := startRow + 1; row <= endRow; row++ {
result = append(result, array[row][endCol])
}
for col := endCol - 1; col >= startCol; col-- {
// If there is a single row at the middle of the matrix, at this
// point we have already appended it to the result's array in the first loop.
if startRow == endRow {
break
}
result = append(result, array[endRow][col])
}
for row := endRow - 1; row > startCol; row-- {
// If there is a single column in the middle of the matrix, at this
// point we have already appended it to the result's array in the second loop.
if startCol == endCol {
break
}
result = append(result, array[row][startCol])
}
startRow++
startCol++
endRow--
endCol--
}
return result
}
func main() {
want := []int{1, 2, 3, 4}
got := spiralTraverse([][]int{[]int{1, 2}, []int{4, 3}})
if !reflect.DeepEqual(want, got) {
fmt.Printf("want: %v, got: %v\n", want, got)
}
// Edge case: single row in th middle of the matrix.
want = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
got = spiralTraverse([][]int{[]int{1, 2, 3, 4}, []int{10, 11, 12, 5}, []int{9, 8, 7, 6}})
if !reflect.DeepEqual(want, got) {
fmt.Printf("want: %v, got: %v\n", want, got)
}
// Edge case: single column in th middle of the matrix.
want = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
got = spiralTraverse([][]int{[]int{1, 2, 3}, []int{12, 13, 4}, []int{11, 14, 5}, []int{10, 15, 6}, []int{9, 8, 7}})
if !reflect.DeepEqual(want, got) {
fmt.Printf("want: %v, got: %v\n", want, got)
}
}