Skip to content

Commit 81b695b

Browse files
committed
测试
1 parent d4dece3 commit 81b695b

File tree

3 files changed

+10118
-39
lines changed

3 files changed

+10118
-39
lines changed
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import { TreeNode } from "../mod.ts";
22

33
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
4-
if (preorder.length === 0) return null;
5-
const i = inorder.findIndex((x) => x === preorder[0]);
6-
7-
return new TreeNode(
8-
preorder[0],
9-
buildTree(preorder.slice(1, i + 1), inorder.slice(0, i)),
10-
buildTree(preorder.slice(i + 1), inorder.slice(i + 1)),
11-
);
4+
const map = new Map<number, number>();
5+
for (let i = 0; i < inorder.length; i++) {
6+
map.set(inorder[i], i);
7+
}
8+
function helper(
9+
pStart: number,
10+
pEnd: number,
11+
iStart: number,
12+
iEnd: number,
13+
): TreeNode | null {
14+
if (pStart > pEnd) {
15+
return null;
16+
}
17+
const rootVal = preorder[pStart];
18+
const root = new TreeNode(rootVal);
19+
const mid = map.get(rootVal) ?? 0;
20+
const leftnum = mid - iStart;
21+
root.left = helper(pStart + 1, pStart + leftnum, iStart, mid - 1);
22+
root.right = helper(pStart + leftnum + 1, pEnd, mid + 1, iEnd);
23+
return root;
24+
}
25+
return helper(0, preorder.length - 1, 0, inorder.length - 1);
1226
}
1327
export default buildTree;

permutations-ii/index.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// const isEqual = _.isEqual;
2-
31
import { uniqBy } from "../deps.ts";
42

5-
// const uniqBy = _.uniqBy;
63
export default function permuteUnique(nums: number[]): number[][] {
74
if (nums.length == 0) {
85
return [];
@@ -11,19 +8,10 @@ export default function permuteUnique(nums: number[]): number[][] {
118
return [nums];
129
}
1310

14-
// const visited = new Set<string>();
1511
const result: number[][] = [];
1612

1713
permute_index(nums, (numsres: number[]) => {
18-
// const numsres = indexs.map((v) => nums[v]);
19-
// const numsres = indexs;
20-
// const key = numsres.join(",");
21-
// if (visited.has(key)) {
22-
// return;
23-
// } else {
24-
// visited.add(key);
2514
result.push(numsres);
26-
// }
2715
});
2816
return uniqBy(result, (a: number[]) => Array.prototype.join.call(a, ","));
2917
}
@@ -37,39 +25,21 @@ function permute_index(nums: number[], output: (indexs: number[]) => void) {
3725
output([0].map((v) => nums[v]));
3826
return;
3927
}
40-
// if (k === 2) {
41-
// // output([0, 1].map((v) => nums[v]));
42-
// // output([1, 0].map((v) => nums[v]));
43-
// const arrays: number[][] = uniqBy(
44-
// [
45-
// [1, 0],
46-
// [0, 1],
47-
// ].map((a) => a.map((v) => nums[v])),
48-
// isEqual
49-
// );
50-
// console.log(arrays)
51-
// for (const res of arrays) {
52-
// output(res);
53-
// }
54-
// return;
55-
// }
5628

5729
permute_index(nums.slice(0, -1), (indexs) => {
5830
const arrays: number[][] = [];
5931
for (let i = 0; i < indexs.length + 1; i++) {
6032
const b = Array.from(indexs);
6133
b.splice(i, 0, nums.slice(-1)[0]);
6234
arrays.push(b);
63-
// output(b);
6435
}
65-
// console.log(arrays);
36+
6637
for (
6738
const res of uniqBy(
6839
arrays,
6940
(a: number[]) => Array.prototype.join.call(a, ","),
7041
)
7142
) {
72-
// console.log(res);
7343
output(res);
7444
}
7545
});

0 commit comments

Comments
 (0)