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 e503702

Browse files
add: Permutations
1 parent 1b3f1d9 commit e503702

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
return prefix;
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+
let permute = function(nums) {
12+
let numlen = nums.length,
13+
res = [];
14+
15+
if (numlen === 0) {
16+
return res;
17+
} else if (numlen === 1) {
18+
res.push(nums);
19+
return res;
20+
}
21+
22+
return subpermute([], nums);
23+
};
24+
25+
let subpermute = function(base, nums) {
26+
let numlen = nums.length,
27+
res = [];
28+
29+
if (numlen === 1) {
30+
return [ base.concat(nums) ];
31+
}
32+
33+
for (let i=0; i<numlen; i++) {
34+
let subarray = [];
35+
for (let j=0; j<numlen; j++) {
36+
if (i===j) {
37+
continue;
38+
}
39+
subarray.push(nums[j]);
40+
}
41+
42+
let newbase = base.concat( [nums[i]] );
43+
res = res.concat( subpermute(newbase, subarray) );
44+
}
45+
46+
return res;
47+
};

0 commit comments

Comments
(0)

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