LeetCode 31. Next Permutation
来源:互联网 发布:jquery 获取数组元素 编辑:程序博客网 时间:2024/06/06 16:24
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 ; }};
0 0
- [LeetCode]31.Next Permutation
- LeetCode --- 31. Next Permutation
- [Leetcode] 31. Next Permutation
- LeetCode 31.Next Permutation
- [leetcode] 31.Next Permutation
- [leetcode] 31. Next Permutation
- *LeetCode 31. Next Permutation
- Leetcode 31. Next Permutation
- leetcode 31. Next Permutation
- 31. Next Permutation LeetCode
- leetcode 31. Next Permutation
- leetcode 31. Next Permutation
- [LeetCode]31. Next Permutation
- [LeetCode]31. Next Permutation
- LeetCode 31. Next Permutation
- 【leetcode】31. Next Permutation
- LeetCode *** 31. Next Permutation
- leetcode 31. Next Permutation
- RGBDSLAM 运行出错:terminate called after throwing an instance of 'pcl::IOException'
- 20170217. OpenGL3 (绘制几何图形)
- PHP 生成短网址
- RStudio创建R程序包
- RTMP直播应用与延时分析
- LeetCode 31. Next Permutation
- BigDecimal 的那些坑事儿
- SmartRoute之远程接口调用和负载
- 学习资料
- 【Linux】信号量
- Struts 从项目中下载文件
- 用Java访问带有Kerberos认证的HBase
- smart210重定位代码到IRAM+0x4000
- Canvas绘制时钟