File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments