deno.land / x / masx200_leetcode_test@10.6.5 / permutations-ii / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { uniqBy } from "../deps.ts";
export default function permuteUnique(nums: number[]): number[][] { if (nums.length == 0) { return []; } if (nums.length == 1) { return [nums]; }
const result: number[][] = [];
permute_index(nums, (numsres: number[]) => { result.push(numsres); }); return uniqBy(result, (a: number[]) => Array.prototype.join.call(a, ","));}
function permute_index(nums: number[], output: (indexs: number[]) => void) { const k = nums.length; if (k === 0) { return; } if (k === 1) { output([0].map((v) => nums[v])); return; }
permute_index(nums.slice(0, -1), (indexs) => { const arrays: number[][] = []; for (let i = 0; i < indexs.length + 1; i++) { const b = Array.from(indexs); b.splice(i, 0, nums.slice(-1)[0]); arrays.push(b); }
for ( const res of uniqBy( arrays, (a: number[]) => Array.prototype.join.call(a, ","), ) ) { output(res); } });}
masx200_leetcode_test

Version Info

Tagged at
a year ago