forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
54-Spiral-Matrix.kt
51 lines (38 loc) · 1.05 KB
/
54-Spiral-Matrix.kt
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
package kotlin
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
var res = mutableListOf<Int>()
var top = 0
var right = matrix.first().size
var left = 0
var bottom = matrix.size
while (top < bottom && left < right) {
//get every i in the top row
for (i in left until right) {
res.add(matrix[top][i])
}
top += 1
// get every i in the right col
for (i in top until bottom) {
res.add(matrix[i][right - 1])
}
right -= 1
if (!(left < right && top < bottom)) {
break
}
//get every i in the bottom row
for (i in right - 1 downTo left) {
res.add(matrix[bottom - 1][i])
}
bottom -= 1
//get every i in the left col
for (i in bottom - 1 downTo top) {
res.add(matrix[i][left])
}
left += 1
}
return res
}
fun main() {
val matrix = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
println(spiralOrder(matrix))
}