-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
62 lines (61 loc) · 1.58 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @lc app=leetcode id=31 lang=javascript
*
* [31] Next Permutation
*
* https://leetcode.com/problems/next-permutation/description/
*
* algorithms
* Medium (29.95%)
* Total Accepted: 209K
* Total Submissions: 698K
* Testcase Example: '[1,2,3]'
*
* Implement next permutation, which rearranges numbers into the
* lexicographically next greater permutation of numbers.
*
* If such arrangement is not possible, it must rearrange it as the lowest
* possible order (ie, sorted in ascending order).
*
* The replacement must be in-place and use only constant extra memory.
*
* Here are some examples. Inputs are in the left-hand column and its
* corresponding outputs are in the right-hand column.
*
* 1,2,3 → 1,3,2
* 3,2,1 → 1,2,3
* 1,1,5 → 1,5,1
*
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
let i = nums.length - 2;
while (nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
let j = nums.length - 1;
while (j > 0 && nums[j] <= nums[i]) {
j--;
}
[nums[j], nums[i]] = [nums[i], nums[j]];
}
let start = i + 1;
let end = nums.length - 1;
while (start < end) {
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
return nums;
};
console.log(nextPermutation([1,5,1]));
module.exports = {
id:'31',
title:'Next Permutation',
url:'https://leetcode.com/problems/next-permutation/description/',
difficulty:'medium',
};