-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
41 lines (34 loc) · 1.04 KB
/
index.js
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
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
if(nums.length > 1){
let end = false;
let len = nums.length;
for(var i = nums.length-2; i>=0; i--){
for(var j = len -1; j> i; j--){
if(nums[i]<nums[j]){
let tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
var sn = nums.splice(i+1).sort(function(a,b){
return a-b;
});
sn.map(function(i){
nums.push(i);
})
j = -1;
i = -1;
end = true;
}
}
continue;
}
if(!end){
nums.sort(function(a,b){
return a-b;
})
}
}
};