Skip to content

Commit 1b31734

Browse files
committed
Next Permutation: AC
1 parent df3b036 commit 1b31734

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode.medium.page1;
2+
3+
import java.util.Arrays;
4+
5+
public class NextPermutation {
6+
public void nextPermutation(int[] nums) {
7+
int len = nums.length;
8+
int i = len - 1;
9+
10+
while (i > 0 && nums[i - 1] >= nums[i]) i--;
11+
12+
if (i != 0) {
13+
int j = i;
14+
while (j < len && nums[i - 1] < nums[j]) j++;
15+
16+
int tmp = nums[i - 1];
17+
nums[i - 1] = nums[j - 1];
18+
nums[j - 1] = tmp;
19+
}
20+
21+
Arrays.sort(nums, i, nums.length);
22+
}
23+
}

0 commit comments

Comments
 (0)