[LeetCode] Next Permutation
2014-06-26 23:35
309 查看
题目描述
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.
解题思路:
首先举个例子.35421->41235.那么我们是怎么找到下一个排列的呢?
1)从后往前找到第一个Ai,Aj,Ai<Aj.针对例子,找到3,5.这说明了3以后的应该是子序列全排列的最后一个排列,因为5后面都是逆序的.对吧?
2)继续从后往前寻找,找到第一个比Ai大的数,与Ai交换,毕竟Ai后面已经是全排序到头了,下一个全排序只能把Ai变大,对吧? 针对例子,找到了4,交换3,4.例子变成了45321.4后面的序列顺序依然是一个逆序.至于为什么?读者仔细思量一下就很容易明白啦.
3)接下来就很容易了,把i位置之后的数逆序一下就OK啦.也就是45321->41235.
解题代码:
public void nextPermutation(int[] num) {
int j;
for( j=num.length-1;j>0&&num[j-1]>=num[j];j--);//找i j,
if(j==0)reverse(num,0,num.length-1);//如果已经是全排列的最后一个,直接逆序吧
else
{
int i=j-1;
int k;
for(k=num.length-1;num[k]<=num[i];k--);//从后往前找第一个比ai大的数
swap(num,i,k);//交换
reverse(num,j,num.length-1);//把i后面的数逆序一下
}
return ;
}
public void swap(int[] num,int i,int j)
{
int tmp=num[i];
num[i]=num[j];
num[j]=tmp;
return ;
}
public void reverse(int[] num,int i,int j)
{
while(i<j)
{
swap(num,i++,j--);
}
return ;
}
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
解题思路:
首先举个例子.35421->41235.那么我们是怎么找到下一个排列的呢?
1)从后往前找到第一个Ai,Aj,Ai<Aj.针对例子,找到3,5.这说明了3以后的应该是子序列全排列的最后一个排列,因为5后面都是逆序的.对吧?
2)继续从后往前寻找,找到第一个比Ai大的数,与Ai交换,毕竟Ai后面已经是全排序到头了,下一个全排序只能把Ai变大,对吧? 针对例子,找到了4,交换3,4.例子变成了45321.4后面的序列顺序依然是一个逆序.至于为什么?读者仔细思量一下就很容易明白啦.
3)接下来就很容易了,把i位置之后的数逆序一下就OK啦.也就是45321->41235.
解题代码:
public void nextPermutation(int[] num) {
int j;
for( j=num.length-1;j>0&&num[j-1]>=num[j];j--);//找i j,
if(j==0)reverse(num,0,num.length-1);//如果已经是全排列的最后一个,直接逆序吧
else
{
int i=j-1;
int k;
for(k=num.length-1;num[k]<=num[i];k--);//从后往前找第一个比ai大的数
swap(num,i,k);//交换
reverse(num,j,num.length-1);//把i后面的数逆序一下
}
return ;
}
public void swap(int[] num,int i,int j)
{
int tmp=num[i];
num[i]=num[j];
num[j]=tmp;
return ;
}
public void reverse(int[] num,int i,int j)
{
while(i<j)
{
swap(num,i++,j--);
}
return ;
}
相关文章推荐
- LeetCode: Interleaving String
- Leetcode Valid Palindrome
- [LeetCode] 3Sum Closest
- [leetcode] Palindrome Partitioning II
- (leetcode) Best Time to Buy and Sell Stock II
- *(leetcode) Binary Tree Level Order Traversal (tree)
- [Leetcode]Reverse Integer
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- 【leetcode c++】13 Roman to Integer
- [leetcode-33]Search in Rotated Sorted Array(C)
- LeetCode----Sort Colors
- leetcode18 4sum
- [Leetcode题目]160. Intersection of Two Linked Lists
- LeetCode-60-Permutation Sequence(找规律)-Medium
- LeetCode——016
- LeetCode 46 Permutations
- Leetcode 16. 3Sum Closest (Medium) (cpp)
- LeetCode 140 Word Break II (DFS 分词方法 推荐)
- LeetCode 456. 132 Pattern
- Leetcode 172 Factorial Trailing Zeroes