Sha256: 1f94d7c6050421ee7edba681824e398b7b705895febbe0a3b15c033b4c324445
Contents?: true
Size: 977 Bytes
Versions: 42
Compression:
Stored size: 977 Bytes
Contents
class SpiralMatrix { static ofSize(size: number) { const spiral = Array(size).fill(Array<number>()).map(() => Array<number>(size).fill(0)) const totalNumbers = size ** 2 let currentNumber = 1 let topLeft = 0 let bottomRight = size - 1 while (currentNumber <= totalNumbers) { for (let x = topLeft; x <= bottomRight; x++) { spiral[topLeft][x] = currentNumber++ } for (let y = topLeft + 1; y <= bottomRight; y++) { spiral[y][bottomRight] = currentNumber++ } for (let x = bottomRight - 1; x >= topLeft; x--) { spiral[bottomRight][x] = currentNumber++ } for (let y = bottomRight - 1; y >= topLeft + 1; y--) { spiral[y][topLeft] = currentNumber++ } topLeft++ bottomRight-- } return spiral } } export default SpiralMatrix
Version data entries
42 entries across 42 versions & 1 rubygems