leetcode_31. Next Permutation
2017-04-10 20:10
369 查看
https://leetcode.com/problems/next-permutation/#/description
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.
Subscribe to see which companies asked this question.
题意:给你一个数组,问他的下一个排列是什么。。。
思路:看几组例子就可以知道了~~
1 2 3 5 4 ------> 1 2 4 3 5
1 2 5 3 4 ------> 1 3 2 4 5
1 3 2 5 4 ------> 1 3 4 2 5
所以,规律就是从后面往前找,找到一个nums[i-1]<nums[i],然后再去后面找一个最小的比nums[i-1]大的,再对i~end的位置进行排序~~详情见代码
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int length = nums.size();
int pos=-1;
for(int i=length-1;i>0;i--)
{
if(nums[i-1]<nums[i])
{
pos=i-1;
break;
}
}
if(pos==-1)
{
sort(nums.begin(),nums.end());
return;
}
int Min=1<<30;
int p=0;
for(int i=pos+1;i<length;i++)
{
if(Min>nums[i]&&nums[i]>nums[pos])
{
Min=nums[i];
p=i;
}
}
int t=nums[p];
nums[p]=nums[pos];
nums[pos]=t;
sort(nums.begin()+pos+1,nums.end());
}
};
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
Subscribe to see which companies asked this question.
题意:给你一个数组,问他的下一个排列是什么。。。
思路:看几组例子就可以知道了~~
1 2 3 5 4 ------> 1 2 4 3 5
1 2 5 3 4 ------> 1 3 2 4 5
1 3 2 5 4 ------> 1 3 4 2 5
所以,规律就是从后面往前找,找到一个nums[i-1]<nums[i],然后再去后面找一个最小的比nums[i-1]大的,再对i~end的位置进行排序~~详情见代码
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int length = nums.size();
int pos=-1;
for(int i=length-1;i>0;i--)
{
if(nums[i-1]<nums[i])
{
pos=i-1;
break;
}
}
if(pos==-1)
{
sort(nums.begin(),nums.end());
return;
}
int Min=1<<30;
int p=0;
for(int i=pos+1;i<length;i++)
{
if(Min>nums[i]&&nums[i]>nums[pos])
{
Min=nums[i];
p=i;
}
}
int t=nums[p];
nums[p]=nums[pos];
nums[pos]=t;
sort(nums.begin()+pos+1,nums.end());
}
};
相关文章推荐
- LeetCode 31.Next Permutation & 33.Search in Rotated Sorted Array
- leetCode(31):Combination Sum III 分类: leetCode 2015-07-07 10:23 163人阅读 评论(0) 收藏
- LeetCode 31. Next Permutation
- [LeetCode]31 Next Permutation
- [LeetCode]31.nextPermutation
- [LeetCode]31. Next Permutation
- 【leetcode】【31】Next Permutation
- leetcode——31——Next Permutation
- LeetCode-31-Next Permutation(C语言实现)
- leetcode: 31. Next Permutation
- leetcode 31:Next Permutation 原理剖析
- leetcode 31. Next Permutation
- LeetCode---(31) Next Permutation
- leetCode(31):Combination Sum III
- Leetcode 31. Next Permutation
- LeetCode 31
- [leetcode 31] Next Permutation
- LeetCode 31 Next Permutation (下一排列)
- leetcode 31. Next Permutation
- 31_leetcode_ Insert Interval