-
Notifications
You must be signed in to change notification settings - Fork 0
/
snail.go
48 lines (40 loc) · 1.08 KB
/
snail.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
package kata
func Snail(snaipMap [][]int) []int {
// Check if the matrix is empty
if len(snaipMap) == 0 {
return []int{}
}
// Initialize the result slice
result := []int{}
// Initialize the boundaries
top, bottom := 0, len(snaipMap)-1
left, right := 0, len(snaipMap[0])-1
// Traverse the matrix in a spiral order
for top <= bottom && left <= right {
// Traverse from left to right along the top boundary
for i := left; i <= right; i++ {
result = append(result, snaipMap[top][i])
}
top++
// Traverse from top to bottom along the right boundary
for i := top; i <= bottom; i++ {
result = append(result, snaipMap[i][right])
}
right--
// Traverse from right to left along the bottom boundary, if still within bounds
if top <= bottom {
for i := right; i >= left; i-- {
result = append(result, snaipMap[bottom][i])
}
bottom--
}
// Traverse from bottom to top along the left boundary, if still within bounds
if left <= right {
for i := bottom; i >= top; i-- {
result = append(result, snaipMap[i][left])
}
left++
}
}
return result
}