-
Notifications
You must be signed in to change notification settings - Fork 0
/
(Medium) 59. Spiral Matrix II.kt
58 lines (56 loc) · 1.67 KB
/
(Medium) 59. Spiral Matrix II.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
52
53
54
55
56
57
58
/*
(Medium) 59. Spiral Matrix II.kt
Your runtime beats 100.00 % of kotlin submissions.
Your memory usage beats 57.78 % of kotlin submissions.
*/
class Solution {
fun generateMatrix(n: Int): Array<IntArray> {
var matrix = Array(n, {IntArray(n){i -> -1}})
var lBound = 0
var rBound = n - 1
var uBound = 0
var dBound = n - 1
var count = 1
var direction = 0
var i = 0
var j = 0
while(count <= n*n){
matrix[i][j] = count
count = count + 1
if(direction % 4 == 0){ // going right
if(j + 1 <= rBound){
j = j + 1
}else{
direction = direction + 1
uBound = uBound + 1
i = i + 1
}
}else if(direction % 4 == 1){ // going down
if(i + 1 <= dBound){
i = i + 1
}else{
direction = direction + 1
rBound = rBound - 1
j = j - 1
}
}else if(direction % 4 == 2){ // going left
if(j - 1 >= lBound){
j = j - 1
}else{
direction = direction + 1
dBound = dBound - 1
i = i - 1
}
}else{ // going up
if(i - 1 >= uBound){
i = i - 1
}else{
direction = direction + 1
lBound = lBound + 1
j = j + 1
}
}
}
return matrix
}
}