-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xjq
committed
Dec 2, 2022
1 parent
857a080
commit f053775
Showing
22 changed files
with
299 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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"} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
对象合并 | ||
|
||
深度合并两个对象, 并且修改新对象不会影响原对象, 相同的属性则以 b 对象为主 | ||
深度合并两个对象可枚举属性, 并且修改新对象不会影响原对象, 相同的属性则以 b 对象为主 | ||
|
||
用例 1: | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
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; | ||
} | ||
``` |
This file contains 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
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; | ||
} |
This file contains 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
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] | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 树的扁平化 | ||
* | ||
* @export | ||
* @param {*} tree | ||
* @return {*} | ||
*/ | ||
export default function flattenTree(tree) { | ||
return []; | ||
} |
This file contains 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
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]); | ||
}); |
This file contains 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
This file contains 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
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 | ||
); | ||
} |
This file contains 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
This file contains 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
This file contains 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
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); | ||
}); |
This file contains 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
This file contains 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
Oops, something went wrong.