Created
November 1, 2020 04:22
-
-
Save luojiyin1987/746e64234262c02b250e7f0b93d81271 to your computer and use it in GitHub Desktop.
简化 逻辑判断的新写法, && 用来执行,不是单纯的判断
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {number[][]} | |
| */ | |
| var generateMatrix = function(n) { | |
| let b = [0, n - 1, 0, n - 1], x = 0, y = 0, i = 0, d = '→', r = new Array(n).fill(0).map(_=>new Array(n).fill(0)) | |
| while (i++ < Math.pow(n, 2)) { | |
| d === '→' && (r[y][x++] = i, x === b[1] && (d = '↓', ++b[2])) || | |
| d === '↓' && (r[y++][x] = i, y === b[3] && (d = '←', b[1]--)) || | |
| d === '←' && (r[y][x--] = i, x === b[0] && (d = '↑', b[3]--)) || | |
| d === '↑' && (r[y--][x] = i, y === b[2] && (d = '→', ++b[0])) | |
| } | |
| return r | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment