-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0031-next-permutation.js
52 lines (45 loc) · 1.05 KB
/
0031-next-permutation.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
42
43
44
45
46
47
48
49
50
51
52
/**
* 31. Next Permutation
* https://leetcode.com/problems/next-permutation/
* Difficulty: Medium
*
* Implement next permutation, which rearranges numbers into the
* lexicographically next greater permutation of numbers.
*
* If such an arrangement is not possible, it must rearrange it
* as the lowest possible order (i.e., sorted in ascending order).
*
* The replacement must be in place and use only constant extra memory.
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
let i = nums.length - 2;
while (i >= 0 && nums[i + 1] <= nums[i]) {
i--;
}
if (i >= 0) {
let cursor = nums.length - 1;
while (nums[cursor] <= nums[i]) {
cursor--;
}
swap(nums, i, cursor);
}
reverse(nums, i + 1);
};
function reverse(nums, start) {
let i = start;
let j = nums.length - 1;
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
function swap(nums, i, j) {
const temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}