leet-code/src/main/java/leetcode/editor/cn/NextPermutation.java
2021-07-15 14:28:20 +08:00

96 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
//
// 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。
//
// 必须 原地 修改,只允许使用额外常数空间。
//
//
//
// 示例 1
//
//
//输入nums = [1,2,3]
//输出:[1,3,2]
//
//
// 示例 2
//
//
//输入nums = [3,2,1]
//输出:[1,2,3]
//
//
// 示例 3
//
//
//输入nums = [1,1,5]
//输出:[1,5,1]
//
//
// 示例 4
//
//
//输入nums = [1]
//输出:[1]
//
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 100
// 0 <= nums[i] <= 100
//
// Related Topics 数组 双指针
// 👍 1221 👎 0
package leetcode.editor.cn;
//31:下一个排列
public class NextPermutation {
public static void main(String[] args) {
//测试代码
Solution solution = new NextPermutation().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void nextPermutation(int[] nums) {
if (nums.length < 2) {
return;
}
int i = nums.length - 2;
for (; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
break;
}
}
if (i != -1) {
int j = i + 1;
for (; j < nums.length; j++) {
if (nums[j] <= nums[i]) {
break;
}
}
swap(nums, i, j - 1);
}
int start = i + 1;
int end = nums.length - 1;
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}