Android大法好！！

# leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation

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, do not allocate 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

public class Solution {
public void nextPermutation(int[] nums) {
if(nums.length <= 1){
return;
}
for(int i = nums.length - 1;i > 0; i--){
if(nums[i] > nums[i-1]){//如果nums[i] > nums[i-1]
//再从后往前判断有否数字比nums[i-1]大
int j = nums.length - 1;
for(; j >= i -1;j--){
if(nums[j] > nums[i-1]){
break;//如有，则结束循环，保留j的值
}
}
//将nums[j]和nums[i-1]交换
int temp = nums[j];
nums[j] = nums[i-1];
nums[i-1] = temp;

//从i开始反序(即交换位置的地方开始反序)
for(j = 0; j < (nums.length - (i))/2;j++){
int k = nums.length - 1 - j;
int m = nums[j+i];
nums[j+i] = nums[k];
nums[k] = m;
}
return;
}
}
//如果到最后没有交换的数据，则说明是降序排列，需要升序排列
for(int i = 0; i < nums.length/2;i++){
int j = nums.length - 1 - i;//双指针排序，交换首尾对应的两两数据即可
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

#### Leetcode刷题记——31. Next Permutation(下一个排列)

2016-11-18 20:10:21

#### 字典序排序问题

2017-02-07 20:07:52

#### 排列生成算法--字典序法

2013-07-30 21:24:29

#### 算法练习 - 字符串的全排列(字典序排列)

2015-12-10 12:51:15

#### 按字典序排序

2017-04-30 18:33:00

#### 字典序问题（排列组合）

2016-09-07 08:24:19

#### C++ 字典排序 原理与实现

2015-07-03 12:03:29

#### 字符串按照字典序排序

2016-05-17 12:33:31

#### 字典排序

2016-07-24 20:01:19

#### 字典序法生成全排列算法的证明

2012-03-21 00:33:02