matrix spiral
problem
solution
function matrixSpiral(n) {
const results = []
for(let i = 0; i < n; i++) {
results.push([])
}
let counter = 1
let startColumn = 0
let endColumn = n - 1
let startRow = 0
let endRow = n - 1
while (startColumn <= endColumn && startRow <= endRow ) {
for(let i = startColumn; i <= endColumn; i++) {
result[startColumn][i] = counter
counter++
}
startRow++
for(let i = startRow; i <= endRow; i++) {
result[i][endColumn] = counter
counter++
}
endColumn--
for(let i = endRow; i >= startRow; i--) {
result[endRow][i] = counter
counter++
}
endRow--
for(let i = endRow; i >= startRow; i--) {
result[i][startColumn] = counter
counter++
}
startColumn++
}
return results
}discussion
Last updated