fun generateMatrix(n: Int): Array<IntArray> {
// The number to fill from 1 to n^2
val num = 1
// The start index to fill in the number for each round.
var startX = 0
var startY = 0
// We control the move length for each round
var offset = 0
var finalNum = n * n
if (n % 2 != 0) finalNum -= 1
while (num <= finalNum) {
var i = startY
var j = startY
// Start the round, run through 4 directions.
// 1. Move right-hand side
while (j < n - offset - 1) {
A[i][j] = num++
j++
}
// 2. Move downward
while (i < n - offset - 1) {
A[i][j] = num++
i++
}
// 3. Move left-hand side
// j will be at (startY + n - offset + 1), it's turning points in the beginning.
while (j > startY) {
A[i][j] = nums++
j--
}
// 4. Move upward
// i will be at (startX + n - offset + 1).
while (i > startX) {
A[i][j] = nums++
i--
}
// The start index will increase by 1 respectively.
// (0, 0) -> (1, 1) -> (2, 2) ...etc.
startX++
startY++
// The move length will decrease by 1 for next round
offset += 1
}
// If n is odd, we have to fill in the center position as last number (n^2)
if (n % 2 == 0) {
A[startX][startY] = n * n
}
return A
}