Skip to content

Commit

Permalink
feat: 新增 flattenTree 题目
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq committed Dec 2, 2022
1 parent 857a080 commit f053775
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 21 deletions.
2 changes: 1 addition & 1 deletion qs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const args = process.argv.slice(2);

const isProd = !!args.find((arg) => arg === 'prod');

let qsToken = args.find((arg) => /QS_TOKEN=/.test(arg));
let qsToken = args.find((arg) => /QS_TOKEN=/.test(arg)) || '';
qsToken = qsToken.replace(/QS_TOKEN=/, '');

const cacheFile = isProd ? 'cache.prod.json' : 'cache.json';
Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/amountThousandthsFormat/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
1 change: 1 addition & 0 deletions question/FrontEnd/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"amountThousandthsFormat":"f1be069939db2f6677b73ff7a57cfb11e20c574b409a1755549a2adf84d0d879","deepClone":"cc3ded25433e3a3ec9cfb3cc36ca20ee5f161bc98935ee2c4fafaa75794892fa","deepMerge":"8d4e5060036a6331f5ce7235994615b04c0cd24182a6b030eb443f2efa8825f7","flatten":"76a30d04acdf5c9d72d9562dffee30f0812f4af7475ddff8dd4c51f53c054302","indexOf":"9367eaf8e0cdd40a39441e412069109deb763aefbbcfce26a813063ba579c38b","ipAddress":"ce4d1b975319224dbf28fd3161cc71287feb4cafa869b1472e956f5e42b31572","sort":"0726aad693538629bfd8145206aa8380e683b7559994bd794292d59bb0b29574","unique":"c6c4503f7f3995874ab07f60460f064f782b6d07ac8b70a47bd69826cf86f187","compose":"6767dc6b8a228a7e9eaaa4e8349b7ba5d2081d4ae1080692603d7ff52b01e936","curry":"e4823d48ff06235c1130d15295b7e6a64bac875f77adccd5895d6079f9c8a90b","flattenTree":"88a894bf835a36d1e6d02adbd09a7a95dee280364de7790678016e8daa31a85f"}
2 changes: 1 addition & 1 deletion question/FrontEnd/compose/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compose from './index.mjs';
import compose from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/curry/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
3 changes: 1 addition & 2 deletions question/FrontEnd/deepClone/test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _deepClone from './answer.mjs';
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/deepMerge/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
对象合并

深度合并两个对象, 并且修改新对象不会影响原对象, 相同的属性则以 b 对象为主
深度合并两个对象可枚举属性, 并且修改新对象不会影响原对象, 相同的属性则以 b 对象为主

用例 1:

Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/deepMerge/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/flatten/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
39 changes: 39 additions & 0 deletions question/FrontEnd/flattenTree/answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
### 方法一

递归处理

子节点不为空的时候, 递归遍历并拼接至当前结果之后

```js
export default function flattenTree(tree) {
return tree.reduce((acc, cur) => {
const { id, children } = cur;
acc.push(id);
if (children && children.length) {
acc = acc.concat(flattenTree(children));
}
return acc;
}, []);
}
```

### 方法二


```js
export default function flattenTree(tree) {
const stack = [...tree.reverse()];
const ans = [];

while (stack.length) {
const top = stack[stack.length - 1];
stack.pop();
ans.push(top.id);
if (top.children && top.children.length) {
stack.push(...top.children.reverse());
}
}
return ans;
}
```
24 changes: 24 additions & 0 deletions question/FrontEnd/flattenTree/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* interface TreeNode{
* id: number;
* children: TreeNode[]
* }
*
* @export
* @param {TreeNode[]} tree
* @return {number[]}
*/
export default function flattenTree(tree) {
const stack = [...tree.reverse()];
const ans = [];

while (stack.length) {
const top = stack[stack.length - 1];
stack.pop();
ans.push(top.id);
if (top.children && top.children.length) {
stack.push(...top.children.reverse());
}
}
return ans;
}
105 changes: 105 additions & 0 deletions question/FrontEnd/flattenTree/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
扁平化一棵树

输入一棵树, 输出书中 id 的扁平集合

用例 1:

```js
const tree1 = [
{
id: 0,
children: [
{
id: 1,
children: [
{
id: 2,
children: [],
},
],
},
{
id: 3,
children: [],
},
],
},
{
id: 4,
children: [],
},
];

flattenTree(tree1); // [0, 1, 2, 3, 4]
```

用例 2:

```js
const tree2 = [];
flattenTree(tree2); // []
```

用例 3:

```js
const tree3 = [
{
id: 0,
children: [],
},
{
id: 1,
children: [],
},
{
id: 2,
children: [],
},
{
id: 3,
children: [],
},
];
flattenTree(tree3); // [0, 1, 2, 3]
```

用例 4:

```js
const tree4 = [
{
id: 0,
children: [
{
id: 1,
children: [
{
id: 2,
children: [],
},
],
},
{
id: 3,
children: [
{
id: 4,
children: [
{
id: 5,
children: [],
},
],
},
],
},
],
},
{
id: 6,
children: [],
},
];
flattenTree(tree4); // [0, 1, 2, 3, 4, 5, 6]
```
10 changes: 10 additions & 0 deletions question/FrontEnd/flattenTree/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 树的扁平化
*
* @export
* @param {*} tree
* @return {*}
*/
export default function flattenTree(tree) {
return [];
}
100 changes: 100 additions & 0 deletions question/FrontEnd/flattenTree/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import flattenTree from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

const tree1 = [
{
id: 0,
children: [
{
id: 1,
children: [
{
id: 2,
children: [],
},
],
},
{
id: 3,
children: [],
},
],
},
{
id: 4,
children: [],
},
];

it('输入 tree1', () => {
assert.deepEqual(flattenTree(tree1), [0, 1, 2, 3, 4]);
});

const tree2 = [];

it('输入 tree2 空树', () => {
assert.deepEqual(flattenTree(tree2), []);
});

const tree3 = [
{
id: 0,
children: [],
},
{
id: 1,
children: [],
},
{
id: 2,
children: [],
},
{
id: 3,
children: [],
},
];

it('输入 tree3 一颗只有一层节点的树', () => {
assert.deepEqual(flattenTree(tree3), [0, 1, 2, 3]);
});

const tree4 = [
{
id: 0,
children: [
{
id: 1,
children: [
{
id: 2,
children: [],
},
],
},
{
id: 3,
children: [
{
id: 4,
children: [
{
id: 5,
children: [],
},
],
},
],
},
],
},
{
id: 6,
children: [],
},
];

it('输入 tree4', () => {
assert.deepEqual(flattenTree(tree4), [0, 1, 2, 3, 4, 5, 6]);
});
2 changes: 1 addition & 1 deletion question/FrontEnd/indexOf/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
4 changes: 2 additions & 2 deletions question/FrontEnd/ipAddress/answer.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function f(ip) {
return /((2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})/.test(
export default function isIp(ip) {
return /^((2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})$/.test(
ip
);
}
2 changes: 1 addition & 1 deletion question/FrontEnd/ipAddress/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
2 changes: 1 addition & 1 deletion question/FrontEnd/sort/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
4 changes: 2 additions & 2 deletions question/FrontEnd/template/test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

it(': 输入 ', () => {
assert.deepEqual(f(), {});
assert.deepEqual(f(), undefined);
});
2 changes: 1 addition & 1 deletion question/FrontEnd/unique/test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import f from './index.mjs';
import f from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

Expand Down
4 changes: 2 additions & 2 deletions server/src/controller/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class QuestionController {
try {
const container = await questionService.createContainer({
env: CodeEnv.nodejs,
cmd: `cat > index.mjs << 'EOF' ${wrapCode}
cmd: `cat > answer.mjs << 'EOF' ${wrapCode}
cat > test.mjs << 'EOF' ${wrapTestCode}
cat > answer.mjs << 'EOF' ${wrapAnswerCode}
cat > _answer.mjs << 'EOF' ${wrapAnswerCode}
./node_modules/mocha/bin/mocha.js test.mjs -b
`,
});
Expand Down
Loading

0 comments on commit f053775

Please sign in to comment.