LeetCode 31. Next Permutation
2017-02-17 16:32
381 查看
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
answer:class Solution {public:void nextPermutation(vector<int>& nums) {int pre,end;end = nums.size() - 1;if(end == 0) return;pre = end - 1;int temp;bool find = false;while(pre >= 0){if(nums[pre] < nums[end]){int index = nums.size() - 1;while(index > pre && nums[index] <= nums[pre] ){index --;}temp = nums[pre];nums[pre] = nums[index];nums[index] = temp;sort(nums.begin() + pre + 1,nums.end());find = true;break;}pre --;end --;}if(!find) sort(nums.begin(),nums.end());return ;}};
相关文章推荐
- 面试笔试杂项积累-leetcode 31-35
- LeetCode 31.NextPermutation
- LeetCode 31.Next Permutation
- LeetCode(31)-Factorial Trailing Zeroes
- LeetCode 31 - Next Permutation
- Leetcode 之Regular Expression Matching(31)
- LeetCode 31---Next Permutation
- LeetCode 31: Next Permutation
- 【leetcode】Array—— Next Permutation(31)
- 【Leetcode】 Next Permutation(31)
- [leetcode]31 Symmetric Tree
- leetcode 31 -- Next Permutation
- LeetCode(31)--Next Permutation
- 【C++】【LeetCode】31. Next Permutation && 46. Permutations
- LeetCode - 31 - Next Permutation
- leetcode记录 31-60
- 【一天一道LeetCode】#31. Next Permutation
- LeetCode Weekly Contest 31解题思路
- <LeetCode OJ> 31. Next Permutation
- leetcode_31题——Next Permutation(STL)