Skip to content

Commit fcadf7b

Browse files
committed
31. Next Permutation
```Solution.c void reverse(int* nums, int l, int r) { while (l < r) { int temp = nums[l]; nums[l++] = nums[r]; nums[r--] = temp; } } void nextPermutation(int* nums, int numsSize) { int i = numsSize - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { int j = numsSize - 1; while (nums[j] <= nums[i]) j--; int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } reverse(nums, i + 1, numsSize - 1); } ``` ```Solution.cpp class Solution { public: void nextPermutation(vector<int>& nums) { int n = nums.size(), i = n - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { int j = n - 1; while (nums[j] <= nums[i]) j--; swap(nums[i], nums[j]); } reverse(nums.begin() + i + 1, nums.end()); } }; ``` ```Solution.cs public class Solution { public void NextPermutation(int[] nums) { int n = nums.Length, i = n - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { int j = n - 1; while (nums[j] <= nums[i]) j--; (nums[i], nums[j]) = (nums[j], nums[i]); } Array.Reverse(nums, i + 1, n - i - 1); } } ``` ```Solution.dart class Solution { void nextPermutation(List<int> nums) { int i = nums.length - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { int j = nums.length - 1; while (nums[j] <= nums[i]) j--; int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } int l = i + 1, r = nums.length - 1; while (l < r) { int temp = nums[l]; nums[l] = nums[r]; nums[r] = temp; l++; r--; } } } ``` ```Solution.go func nextPermutation(nums []int) { i := len(nums) - 2 for i >= 0 && nums[i] >= nums[i+1] { i-- } if i >= 0 { j := len(nums) - 1 for nums[j] <= nums[i] { j-- } nums[i], nums[j] = nums[j], nums[i] } reverse(nums[i+1:]) } func reverse(nums []int) { l, r := 0, len(nums)-1 for l < r { nums[l], nums[r] = nums[r], nums[l] l++ r-- } } ``` ```Solution.java class Solution { public void nextPermutation(int[] nums) { int n = nums.length, i = n - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { int j = n - 1; while (nums[j] <= nums[i]) j--; int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } reverse(nums, i + 1, n - 1); } private void reverse(int[] nums, int l, int r) { while (l < r) { int temp = nums[l]; nums[l++] = nums[r]; nums[r--] = temp; } } } ``` ```Solution.js /** * @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] >= nums[i + 1]) i--; if (i >= 0) { let j = nums.length - 1; while (nums[j] <= nums[i]) j--; [nums[i], nums[j]] = [nums[j], nums[i]]; } let left = i + 1, right = nums.length - 1; while (left < right) { [nums[left], nums[right]] = [nums[right], nums[left]]; left++; right--; } }; ``` ```Solution.kt class Solution { fun nextPermutation(nums: IntArray): Unit { var i = nums.size - 2 while (i >= 0 && nums[i] >= nums[i + 1]) i-- if (i >= 0) { var j = nums.size - 1 while (nums[j] <= nums[i]) j-- nums[i] = nums[j].also { nums[j] = nums[i] } } nums.reverse(i + 1, nums.size) } private fun IntArray.reverse(l: Int, r: Int) { var left = l var right = r - 1 while (left < right) { this[left] = this[right].also { this[right] = this[left] } left++; right-- } } } ``` ```Solution.php class Solution { /** * @param Integer[] $nums * @return NULL */ function nextPermutation(&$nums) { $n = count($nums); $i = $n - 2; while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) $i--; if ($i >= 0) { $j = $n - 1; while ($nums[$j] <= $nums[$i]) $j--; [$nums[$i], $nums[$j]] = [$nums[$j], $nums[$i]]; } $l = $i + 1; $r = $n - 1; while ($l < $r) { [$nums[$l], $nums[$r]] = [$nums[$r], $nums[$l]]; $l++; $r--; } } } ``` ```Solution.py class Solution: def nextPermutation(self, nums: List[int]) -> None: i = len(nums) - 2 while i >= 0 and nums[i] >= nums[i + 1]: i -= 1 if i >= 0: j = len(nums) - 1 while nums[j] <= nums[i]: j -= 1 nums[i], nums[j] = nums[j], nums[i] nums[i + 1:] = reversed(nums[i + 1:]) ``` ```Solution.rb # @param {Integer[]} nums # @return {Void} Do not return anything, modify nums in-place instead. def next_permutation(nums) i = nums.length - 2 i -= 1 while i >= 0 && nums[i] >= nums[i + 1] if i >= 0 j = nums.length - 1 j -= 1 while nums[j] <= nums[i] nums[i], nums[j] = nums[j], nums[i] end nums[i+1..-1] = nums[i+1..-1].reverse end ``` ```Solution.rs impl Solution { pub fn next_permutation(nums: &mut Vec<i32>) { let n = nums.len(); let mut i = n as i32 - 2; while i >= 0 && nums[i as usize] >= nums[(i + 1) as usize] { i -= 1; } if i >= 0 { let mut j = n as i32 - 1; while nums[j as usize] <= nums[i as usize] { j -= 1; } nums.swap(i as usize, j as usize); } nums[(i + 1) as usize..].reverse(); } } ``` ```Solution.scala object Solution { def nextPermutation(nums: Array[Int]): Unit = { var i = nums.length - 2 while (i >= 0 && nums(i) >= nums(i + 1)) i -= 1 if (i >= 0) { var j = nums.length - 1 while (nums(j) <= nums(i)) j -= 1 val temp = nums(i); nums(i) = nums(j); nums(j) = temp } reverse(nums, i + 1, nums.length - 1) } private def reverse(nums: Array[Int], l: Int, r: Int): Unit = { var left = l; var right = r while (left < right) { val temp = nums(left); nums(left) = nums(right); nums(right) = temp left += 1; right -= 1 } } } ``` ```Solution.swift class Solution { func nextPermutation(_ nums: inout [Int]) { var i = nums.count - 2 while i >= 0 && nums[i] >= nums[i + 1] { i -= 1 } if i >= 0 { var j = nums.count - 1 while nums[j] <= nums[i] { j -= 1 } nums.swapAt(i, j) } nums[(i+1)...].reverse() } } ``` ```Solution.ts /** Do not return anything, modify nums in-place instead. */ function nextPermutation(nums: number[]): void { let i = nums.length - 2; while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) { let j = nums.length - 1; while (nums[j] <= nums[i]) j--; [nums[i], nums[j]] = [nums[j], nums[i]]; } let l = i + 1, r = nums.length - 1; while (l < r) { [nums[l], nums[r]] = [nums[r], nums[l]]; l++; r--; } } ```
1 parent 5a09225 commit fcadf7b

19 files changed

+281
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [**31. Next Permutation**](https://leetcode.com/problems/next-permutation/description/)
2+
3+
A **permutation** of an array of integers is an arrangement of its members into a sequence or linear order.
4+
- For example, for `arr = [1,2,3]`, the following are all the permutations of `arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]`.
5+
6+
The **next permutation** of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the **next permutation** of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
7+
- For example, the next permutation of `arr = [1,2,3] is [1,3,2]`.
8+
- Similarly, the next permutation of `arr = [2,3,1] is [3,1,2]`.
9+
- While the next permutation of `arr = [3,2,1]` is `[1,2,3]` because `[3,2,1]` does not have a lexicographical larger rearrangement.
10+
11+
Given an array of integers `nums`, find the next permutation of `nums`.
12+
13+
The replacement must be [**in place**](http://en.wikipedia.org/wiki/In-place_algorithm) and use only constant extra memory.
14+
15+
#### **Example 1:**
16+
```md
17+
Input: nums = [1,2,3]
18+
Output: [1,3,2]
19+
```
20+
21+
#### **Example 2:**
22+
```md
23+
Input: nums = [3,2,1]
24+
Output: [1,2,3]
25+
```
26+
27+
#### **Example 3:**
28+
```md
29+
Input: nums = [1,1,5]
30+
Output: [1,5,1]
31+
```
32+
33+
#### **Constraints:**
34+
> - `1 <= nums.length <= 100`
35+
> - `0 <= nums[i] <= 100`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void reverse(int* nums, int l, int r) {
2+
while (l < r) {
3+
int temp = nums[l];
4+
nums[l++] = nums[r];
5+
nums[r--] = temp;
6+
}
7+
}
8+
void nextPermutation(int* nums, int numsSize) {
9+
int i = numsSize - 2;
10+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
11+
if (i >= 0) {
12+
int j = numsSize - 1;
13+
while (nums[j] <= nums[i]) j--;
14+
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;
15+
}
16+
reverse(nums, i + 1, numsSize - 1);
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
void nextPermutation(vector<int>& nums) {
4+
int n = nums.size(), i = n - 2;
5+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
6+
if (i >= 0) {
7+
int j = n - 1;
8+
while (nums[j] <= nums[i]) j--;
9+
swap(nums[i], nums[j]);
10+
}
11+
reverse(nums.begin() + i + 1, nums.end());
12+
}
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public void NextPermutation(int[] nums) {
3+
int n = nums.Length, i = n - 2;
4+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
5+
if (i >= 0) {
6+
int j = n - 1;
7+
while (nums[j] <= nums[i]) j--;
8+
(nums[i], nums[j]) = (nums[j], nums[i]);
9+
}
10+
Array.Reverse(nums, i + 1, n - i - 1);
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
void nextPermutation(List<int> nums) {
3+
int i = nums.length - 2;
4+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
5+
if (i >= 0) {
6+
int j = nums.length - 1;
7+
while (nums[j] <= nums[i]) j--;
8+
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;
9+
}
10+
int l = i + 1, r = nums.length - 1;
11+
while (l < r) {
12+
int temp = nums[l]; nums[l] = nums[r]; nums[r] = temp;
13+
l++; r--;
14+
}
15+
}
16+
}

sol/solution/0001-0100/0031/Solution.erl

Whitespace-only changes.

sol/solution/0001-0100/0031/Solution.ex

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func nextPermutation(nums []int) {
2+
i := len(nums) - 2
3+
for i >= 0 && nums[i] >= nums[i+1] {
4+
i--
5+
}
6+
if i >= 0 {
7+
j := len(nums) - 1
8+
for nums[j] <= nums[i] {
9+
j--
10+
}
11+
nums[i], nums[j] = nums[j], nums[i]
12+
}
13+
reverse(nums[i+1:])
14+
}
15+
func reverse(nums []int) {
16+
l, r := 0, len(nums)-1
17+
for l < r {
18+
nums[l], nums[r] = nums[r], nums[l]
19+
l++
20+
r--
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public void nextPermutation(int[] nums) {
3+
int n = nums.length, i = n - 2;
4+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
5+
if (i >= 0) {
6+
int j = n - 1;
7+
while (nums[j] <= nums[i]) j--;
8+
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;
9+
}
10+
reverse(nums, i + 1, n - 1);
11+
}
12+
private void reverse(int[] nums, int l, int r) {
13+
while (l < r) {
14+
int temp = nums[l];
15+
nums[l++] = nums[r];
16+
nums[r--] = temp;
17+
}
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
var nextPermutation = function(nums) {
6+
let i = nums.length - 2;
7+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
8+
if (i >= 0) {
9+
let j = nums.length - 1;
10+
while (nums[j] <= nums[i]) j--;
11+
[nums[i], nums[j]] = [nums[j], nums[i]];
12+
}
13+
let left = i + 1, right = nums.length - 1;
14+
while (left < right) {
15+
[nums[left], nums[right]] = [nums[right], nums[left]];
16+
left++; right--;
17+
}
18+
};

0 commit comments

Comments
 (0)