mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
31.下一个排列 添加Java版本
This commit is contained in:
@ -99,6 +99,24 @@ public:
|
||||
## Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public void nextPermutation(int[] nums) {
|
||||
for (int i = nums.length - 1; i >= 0; i--) {
|
||||
for (int j = nums.length - 1; j > i; j--) {
|
||||
if (nums[j] > nums[i]) {
|
||||
// 交换
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
// [i + 1, nums.length) 内元素升序排序
|
||||
Arrays.sort(nums, i + 1, nums.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Arrays.sort(nums); // 不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
Reference in New Issue
Block a user