Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cb4383

Browse files
update: 47
1 parent 56dc330 commit 4cb4383

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4141
| 43 | [Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [JavaScript](./src/multiply-strings/res.js) | Medium |
4242
| 45 | [Jump Game II](https://leetcode.com/problems/jump-game-ii/) | [JavaScript](./src/jump-game-ii/res.js) | Hard |
4343
| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [JavaScript](./src/permutations/res.js) | Medium |
44+
| 47 | [permutations-ii](https://leetcode.com/problems/permutations-ii/) | [TypeScript](./src/permutations-ii/res.ts) | Medium |
4445
| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js) | Medium |
4546
| 49 | [Group Anagrams](https://leetcode.com/problems/anagrams/) | [JavaScript](./src/anagrams/res.js) | Medium |
4647
| 50 | [powx-n](https://leetcode.com/problems/powx-n/) | [TypeScript](./src/powx-n/res.ts) | Medium |

‎src/permutations-ii/res.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function getUniqueList(numsCountMap: { [key in number]: number }): number[][] {
2+
const result: number[][] = [];
3+
4+
const keys = Object.keys(numsCountMap);
5+
6+
keys.forEach((key) => {
7+
// @ts-ignore
8+
const currentCount = numsCountMap[key];
9+
const currentNum = Number(key);
10+
11+
if (currentCount) {
12+
const newNumsCountMap = { ...numsCountMap };
13+
newNumsCountMap[key]--;
14+
for (let newKey in newNumsCountMap) {
15+
if (!newNumsCountMap[newKey]) {
16+
delete newNumsCountMap[newKey];
17+
}
18+
}
19+
20+
const uniqueList = getUniqueList(newNumsCountMap);
21+
22+
if (!uniqueList.length) {
23+
result.push([currentNum]);
24+
} else {
25+
uniqueList.forEach((item) => {
26+
result.push([currentNum, ...item]);
27+
});
28+
}
29+
}
30+
})
31+
32+
return result;
33+
}
34+
35+
function permuteUnique(nums: number[]): number[][] {
36+
const numsCountMap: { [key in number]: number } = {};
37+
nums.sort((a, b) => a - b);
38+
nums.forEach((num, index) => {
39+
numsCountMap[num] = numsCountMap[num] ? numsCountMap[num] + 1 : 1;
40+
});
41+
42+
if (nums.length <= 1) {
43+
return [nums];
44+
}
45+
46+
return getUniqueList(numsCountMap);
47+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /