g0001_0100.s0059_spiral_matrix_ii.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0059_spiral_matrix_ii
// #Medium #Array #Matrix #Simulation #Data_Structure_II_Day_3_Array
// #2023_07_10_Time_153_ms_(41.70%)_Space_34.2_MB_(89.36%)
class Solution {
fun generateMatrix(n: Int): Array {
var num = 1
var rStart = 0
var rEnd = n - 1
var cStart = 0
var cEnd = n - 1
val spiral = Array(n) { IntArray(n) }
while (rStart <= rEnd && cStart <= cEnd) {
for (k in cStart..cEnd) {
spiral[rStart][k] = num++
}
rStart++
for (k in rStart..rEnd) {
spiral[k][cEnd] = num++
}
cEnd--
if (rStart <= rEnd) {
for (k in cEnd downTo cStart) {
spiral[rEnd][k] = num++
}
}
rEnd--
if (cStart <= cEnd) {
for (k in rEnd downTo rStart) {
spiral[k][cStart] = num++
}
}
cStart++
}
return spiral
}
}