2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 594810f028c0303b75339ad8
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 之字形矩阵
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: zig-zag-matrix
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
“zig-zag”数组是第一个$ N ^ 2 $整数的正方形排列,当数组沿着数组的[反对角线](https://en.wiktionary.org/wiki/antidiagonal)曲折时,数字会逐渐增加。例如,给定“'5”',产生这个数组:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```
|
|
|
|
|
0 1 5 6 14
|
|
|
|
|
2 4 7 13 15
|
|
|
|
|
3 8 12 16 21
|
|
|
|
|
9 11 17 20 22
|
|
|
|
|
10 18 19 23 24
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
编写一个采用Z字形矩阵大小的函数,并将相应的矩阵作为二维数组返回。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix必须是一个功能
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.equal(typeof ZigZagMatrix, 'function');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix应该返回数组
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.equal(typeof ZigZagMatrix(1), 'object');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix应该返回一个nestes数组的数组
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.equal(typeof ZigZagMatrix(1)[0], 'object');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix(1)应返回\[[0]]
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.deepEqual(ZigZagMatrix(1), zm1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix(2)应返回\[[0,1],[2,3]]
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(ZigZagMatrix(2), zm2);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
ZigZagMatrix(5)必须返回指定的矩阵
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.deepEqual(ZigZagMatrix(5), zm5);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const zm1 = [[0]];
|
|
|
|
|
const zm2 = [[0, 1], [2, 3]];
|
|
|
|
|
const zm5 = [
|
|
|
|
|
[0, 1, 5, 6, 14],
|
|
|
|
|
[2, 4, 7, 13, 15],
|
|
|
|
|
[3, 8, 12, 16, 21],
|
|
|
|
|
[9, 11, 17, 20, 22],
|
|
|
|
|
[10, 18, 19, 23, 24]
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function ZigZagMatrix(n) {
|
|
|
|
|
|
|
|
|
|
return [[], []];
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function ZigZagMatrix(n) {
|
|
|
|
|
const mtx = [];
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
mtx[i] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let i = 1;
|
|
|
|
|
let j = 1;
|
|
|
|
|
for (let e = 0; e < n * n; e++) {
|
|
|
|
|
mtx[i - 1][j - 1] = e;
|
|
|
|
|
if ((i + j) % 2 === 0) {
|
|
|
|
|
// Even stripes
|
|
|
|
|
if (j < n) j++;
|
|
|
|
|
else i += 2;
|
|
|
|
|
if (i > 1) i--;
|
|
|
|
|
} else {
|
|
|
|
|
// Odd stripes
|
|
|
|
|
if (i < n) i++;
|
|
|
|
|
else j += 2;
|
|
|
|
|
if (j > 1) j--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mtx;
|
|
|
|
|
}
|
|
|
|
|
```
|