-
Notifications
You must be signed in to change notification settings - Fork 37
/
SpiralTraverse.go
149 lines (136 loc) · 4.37 KB
/
SpiralTraverse.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package spiral_traverse
// important question
// SpiralTraverse
// better solution
func SpiralTraverse(array [][]int) []int {
if len(array) == 0 {
return []int{}
}
result := make([]int, 0)
spiralFill(array, 0, len(array)-1, 0, len(array[0])-1, &result)
return result
}
func spiralFill(array [][]int, startRow, endRow, startCol, endCol int, result *[]int) {
if startRow > endRow || startCol > endCol {
return
}
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-- {
// Handle the edge case when there's a single row
// in the middle of the matrix. In this case, we don't
// want to double-count the values in this row, which
// we've already counted in the first for loop above.
// See Test Case 8 for an example of this edge case.
if startRow == endRow {
break
}
*result = append(*result, array[endRow][col])
}
for row := endRow - 1; row >= startRow+1; row-- {
// Handle the edge case when there's a single column
// in the middle of the matrix. In this case, we don't
// want to double-count the values in this column, which
// we've already counted in the second for loop above.
// See Test Case 9 for an example of this edge case.
if startCol == endCol {
break
}
*result = append(*result, array[row][startCol])
}
spiralFill(array, startRow+1, endRow-1, startCol+1, endCol-1, result)
}
func spiralTraverse1(array [][]int) []int {
if len(array) == 0 {
return []int{}
}
result := make([]int, 0)
startRow, endRow := 0, len(array)-1
startCol, endCol := 0, len(array[0])-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-- {
// Handle the edge case when there's a single row
// in the middle of the matrix. In this case, we don't
// want to double-count the values in this row, which
// we've already counted in the first for loop above.
// See Test Case 8 for an example of this edge case.
if startRow == endRow {
break
}
result = append(result, array[endRow][col])
}
for row := endRow - 1; row > startRow; row-- {
// Handle the edge case when there's a single column
// in the middle of the matrix. In this case, we don't
// want to double-count the values in this column, which
// we've already counted in the second for loop above.
// See Test Case 9 for an example of this edge case.
if startCol == endCol {
break
}
result = append(result, array[row][startCol])
}
startRow++
endRow--
startCol++
endCol--
}
return result
}
// my solution
func spiralTraverse(array [][]int) []int {
var rowSize, colSize int
rowSize = len(array)
colSize = len(array[0])
result := make([]int, rowSize * colSize, rowSize * colSize)
traverseMatrix(0, 0, 0, 0, 0, rowSize, colSize, result, array)
return result
}
func traverseMatrix(idxRow, idxCol, i, rowStart, colStart, rowSize, colSize int, result []int, matrix [][]int) {
if i > len(result) -1 {
return
}
if idxCol == colSize-1 && idxRow == rowStart && i == len(result) -1 {
result[i] = matrix[idxRow][idxCol]
return
}
if idxCol < colSize-1 && idxRow == rowStart {
result[i] = matrix[idxRow][idxCol]
// traverseRight
traverseMatrix(idxRow, idxCol+1, i+1, rowStart, colStart, rowSize, colSize, result, matrix)
}
if idxCol == colSize-1 && idxRow < rowSize-1 {
result[i] = matrix[idxRow][idxCol]
// traverseBelow
traverseMatrix(idxRow+1, idxCol, i+1, rowStart, colStart, rowSize, colSize, result, matrix)
}
if idxCol > colStart && idxRow == rowSize-1 {
result[i] = matrix[idxRow][idxCol]
if rowSize != 1 {
// traverseLeft
traverseMatrix(idxRow, idxCol-1, i+1, rowStart, colStart, rowSize, colSize, result, matrix)
}
}
if idxRow > rowStart && idxCol == colStart {
result[i] = matrix[idxRow][idxCol]
if idxRow-1 == rowStart {
// traverseInside
traverseMatrix(idxRow, idxCol+1, i+1, rowStart+1, colStart+1, rowSize-1, colSize-1, result, matrix)
return
}
if colSize != 1 {
//traverseAbove
traverseMatrix(idxRow-1, idxCol, i+1, rowStart, colStart, rowSize, colSize, result, matrix)
}
}
}