Movatterモバイル変換


[0]ホーム

URL:


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

Commite503702

Browse files
committed
add: Permutations
1 parent1b3f1d9 commite503702

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2222
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[JavaScript](./src/divide-two-integers/res.js)|Medium|
2323
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
2424
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[JavaScript](./src/search-for-a-range/res.js)|Medium|
25+
|46|[Permutations](https://leetcode.com/problems/permutations/)|[JavaScript](./src/permutations/res.js)|Medium|
2526
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)|[JavaScript](./src/rotate-image/res.js)|Medium|
2627
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)|[JavaScript](./src/spiral-matrix/res.js)|Medium|
2728
|66|[Plus One](https://leetcode.com/problems/plus-one/)|[JavaScript](./src/plus-one/res.js)|Easy|

‎src/longest-common-prefix/res.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ let longestCommonPrefix = function(strs) {
3636
}
3737

3838
returnprefix;
39-
};
39+
};
40+
41+
/**
42+
* Another solution: Sort the array first, and then you can simply compare the first and last elements in the sorted array.
43+
*
44+
*/

‎src/permutations/res.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* res.js
3+
*@authors Joe Jiang (hijiangtao@gmail.com)
4+
*@date 2017-04-03 21:52:31
5+
*
6+
* Problem: Given a collection of distinct numbers, return all possible permutations.
7+
*
8+
*@param {number[]} nums
9+
*@return {number[][]}
10+
*/
11+
letpermute=function(nums){
12+
letnumlen=nums.length,
13+
res=[];
14+
15+
if(numlen===0){
16+
returnres;
17+
}elseif(numlen===1){
18+
res.push(nums);
19+
returnres;
20+
}
21+
22+
returnsubpermute([],nums);
23+
};
24+
25+
letsubpermute=function(base,nums){
26+
letnumlen=nums.length,
27+
res=[];
28+
29+
if(numlen===1){
30+
return[base.concat(nums)];
31+
}
32+
33+
for(leti=0;i<numlen;i++){
34+
letsubarray=[];
35+
for(letj=0;j<numlen;j++){
36+
if(i===j){
37+
continue;
38+
}
39+
subarray.push(nums[j]);
40+
}
41+
42+
letnewbase=base.concat([nums[i]]);
43+
res=res.concat(subpermute(newbase,subarray));
44+
}
45+
46+
returnres;
47+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp