https://leetcode.com/problems/next-permutation/
https://www.cnblogs.com/grandyang/p/4428207.html
http://www.programcreek.com/2014/06/leetcode-next-permutation-java/
https://leetcode.com/discuss/36089/sharing-clean-and-easy-understand-java-code-with-explanation
https://leetcode.com/discuss/70881/easiest-java-solution
http://rafal.io/posts/leetcode-30-next-permutation.html
public void nextPermutation(int[] A) { if(A == null || A.length <= 1) return; int i = A.length - 2; while(i >= 0 && A[i] >= A[i + 1]) i--; // Find 1st id i that breaks descending order if(i >= 0) { // If not entirely descending int j = A.length - 1; // Start from the end while(A[j] <= A[i]) j--; // Find rightmost first larger id j swap(A, i, j); // Switch i and j } reverse(A, i + 1, A.length - 1); // Reverse the descending sequence } public void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; }
http://shmilyaw-hotmail-com.iteye.com/blog/2286657
https://leetcode.com/discuss/21321/sharing-my-really-simple-solution-with-explanation
void nextPermutation(vector<int> &num) { for(int i = num.size() - 2; i >= 0; i--){ if(num[i] < num[i + 1]){ int pos; int diff = INT_MAX; for(int j = i + 1; j < num.size(); j++){ if(num[j] > num[i] && abs(num[i] - num[j]) < diff){ diff = abs(num[i] - num[j]); pos = j; } } swap(num[i], num[pos]); sort(num.begin() + i + 1, num.end()); return; } } sort(num.begin(), num.end()); }
In order to find the next permutation of s , we need to (1) identify such index i as large as possible, (2) swap the digit at index i with the smallest larger value behind it, and (3) rearrange the digits after it in non-descending order. Because of (2), (1) can be achieved by identifying the first increasing adjacent pair in the reverse order, where i is the the index of the first digit. Otherwise, there would be no value greater the digit at index i . And after (2), the digits after index i must be in non-increasing order (prove this yourself). What is left is to reverse them.
http://www.cnblogs.com/TenosDoIt/p/3770126.html
1. next large number
2. Next permutation && Previous 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.
Time complexity: O(N)1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
https://www.cnblogs.com/grandyang/p/4428207.html
这道题让我们求下一个排列顺序,有题目中给的例子可以看出来,如果给定数组是降序,则说明是全排列的最后一种情况,则下一个排列就是最初始情况,可以参见之前的博客 Permutations 全排列。我们再来看下面一个例子,有如下的一个数组
1 2 7 4 3 1
下一个排列为:
1 3 1 2 4 7
那么是如何得到的呢,我们通过观察原数组可以发现,如果从末尾往前看,数字逐渐变大,到了2时才减小的,然后我们再从后往前找第一个比2大的数字,是3,那么我们交换2和3,再把此时3后面的所有数字转置一下即可,步骤如下:
1 2 7 4 3 1
1 2 7 4 3 1
1 3 7 4 2 1
1 3 1 2 4 7
void nextPermutation(vector<int> &num) { int i, j, n = num.size(); for (i = n - 2; i >= 0; --i) { if (num[i + 1] > num[i]) { for (j = n - 1; j > i; --j) { if (num[j] > num[i]) break; } swap(num[i], num[j]); reverse(num.begin() + i + 1, num.end()); return; } } reverse(num.begin(), num.end()); }
public void nextPermutation(int[] A) {
if(A == null || A.length <= 1) return;
int i = A.length - 2;
while(i >= 0 && A[i] >= A[i + 1]) i--; // Find 1st id i that breaks descending order
if(i >= 0) { // If not entirely descending
int j = A.length - 1; // Start from the end
while(A[j] <= A[i]) j--; // Find rightmost first larger id j
swap(A, i, j); // Switch i and j
}
reverse(A, i + 1, A.length - 1); // Reverse the descending sequence
}
public void swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
public void reverse(int[] A, int i, int j) {
while(i < j) swap(A, i++, j--);
}
The steps to solve this problem:
1) scan from right to left, find the first element that is less than its previous one.
1) scan from right to left, find the first element that is less than its previous one.
4 5 6 3 2 1 | p
2) scan from right to left, find the first element that is greater than p.
4 5 6 3 2 1 | q
3) swap p and q
4 5 6 3 2 1 swap 4 6 5 3 2 1
4) reverse elements [p+1, nums.length]
4 6 1 2 3 5
https://leetcode.com/discuss/70881/easiest-java-solution
http://rafal.io/posts/leetcode-30-next-permutation.html
public void nextPermutation(int[] A) { if(A == null || A.length <= 1) return; int i = A.length - 2; while(i >= 0 && A[i] >= A[i + 1]) i--; // Find 1st id i that breaks descending order if(i >= 0) { // If not entirely descending int j = A.length - 1; // Start from the end while(A[j] <= A[i]) j--; // Find rightmost first larger id j swap(A, i, j); // Switch i and j } reverse(A, i + 1, A.length - 1); // Reverse the descending sequence } public void swap(int[] A, int i, int j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; }
http://shmilyaw-hotmail-com.iteye.com/blog/2286657
- public void nextPermutation(int[] num) {
- int lastInc = findLastIncr(num);
- if(lastInc < 0)
- reverse(num, 0, num.length - 1);
- else {
- int l = findLastBigger(num, lastInc);
- swap(num, lastInc, l);
- reverse(num, lastInc + 1, num.length - 1);
- }
- }
- private int findLastIncr(int[] num) {
- for(int i = num.length - 1; i >= 1; i--) {
- if(num[i] > num[i - 1])
- return i - 1;
- }
- return -1;
- }
- private int findLastBigger(int[] num, int lastIdx) {
- for(int i = num.length - 1; i > lastIdx; i--)
- if(num[i] > num[lastIdx])
- return i;
- return -1;
- }
比如原排列为1 3 5 4 2,找到第一个数3,其小于后面的数字5,然后找到第一个数字4,其大于3,交换3和4,得到1 4 5 3 2,然后逆转原来3后面的数字,即5 3 2,得到1 4 2 3 5,即为下一个排列。
public void nextPermutation(int[] num) {
int i = 0;
int j = 0;
//From right to left, find the first one that is not in ascending order.
for (i = num.length - 2; i >= 0; i--) {
if (num[i] >= num[i + 1])
continue;
//From right to left, find the first one that is larger than num[i]
for (j = num.length - 1; j > i; j--) {
if (num[j] > num[i])
break;
}
break;
}
//If we find i, swap the number on position i and j
if (i >= 0) {
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
//Reverse the numbers which are on the right of i
int start = i + 1;
int end = num.length - 1;
while (start < end) {
int tmp = num[start];
num[start] = num[end];
num[end] = tmp;
start++;
end--;
}
}
void nextPermutation(vector<int> &num) { for(int i = num.size() - 2; i >= 0; i--){ if(num[i] < num[i + 1]){ int pos; int diff = INT_MAX; for(int j = i + 1; j < num.size(); j++){ if(num[j] > num[i] && abs(num[i] - num[j]) < diff){ diff = abs(num[i] - num[j]); pos = j; } } swap(num[i], num[pos]); sort(num.begin() + i + 1, num.end()); return; } } sort(num.begin(), num.end()); }
In order to find the next permutation of s , we need to (1) identify such index i as large as possible, (2) swap the digit at index i with the smallest larger value behind it, and (3) rearrange the digits after it in non-descending order. Because of (2), (1) can be achieved by identifying the first increasing adjacent pair in the reverse order, where i is the the index of the first digit. Otherwise, there would be no value greater the digit at index i . And after (2), the digits after index i must be in non-increasing order (prove this yourself). What is left is to reverse them.
http://www.cnblogs.com/TenosDoIt/p/3770126.html
- 从最尾端开始往前寻找两个相邻的元素,两者满足i < ii(令第一个元素为i,第二个元素为ii)
- 如果没有找到这样的一对元素则,表明当前的排列是最大的,没有下一个大的排列
- 如果找到,再从末尾开始找出第一个大于i的元素,记为j
- 交换元素i, j,再将ii后面的所有元素颠倒排列(包括ii)
- 如果某个排列没有比他大的下一个排列(即该排列是递减有序的),调用这个函数还是会把原排列翻转,即得到最小的排列
public void nextPermutation(int[] num) {
int n = num.length;
int a;
// Find the first increasing adjacent pair in the reverse order;
// a is the index of the first number of the pair
for (a = n-2; a >= 0; a--)
if (num[a] < num[a+1])
break;
// The array is in non-increasing order;
// already the maximum, goes to the smallest
if (a < 0) {
reverse(num, 0);
return;
}
// min is the index of the smallest number behind num[a]
// that are larger than num[a]
int min = a + 1;
for (int i = a+2; i < n; i++) {
if (num[i] <= num[a])
break;
if (num[i] <= num[min])
min = i;
}
// Swap num[a] and num[min]
int temp = num[a];
num[a] = num[min];
num[min] = temp;
// Reverse num[a+1..]
reverse(num, a+1);
}
1. next large number
2. Next permutation && Previous permutation