31. Next Permutation
来源:互联网 发布:mac地址获取工具 编辑:程序博客网 时间:2024/06/08 08:37
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
class Solution {public: void nextPermutation(vector<int>& nums) { int n = nums.size(); int last = n - 2; while(nums[last] >= nums[last + 1] && last >= 0) last--; if(last < 0) { reverse(nums.begin(), nums.end()); return ;} reverse(nums.end() - (n - 1 - last) , nums.end()); int temp = nums[last]; int i = last + 1; while(nums[i] <= temp) i++; nums[last] = nums[i]; nums[i] = temp; }};
0 0
- [LeetCode]31.Next Permutation
- LeetCode --- 31. Next Permutation
- [Leetcode] 31. Next Permutation
- LeetCode 31.Next Permutation
- 31. Next Permutation
- [leetcode] 31.Next Permutation
- [leetcode] 31. Next Permutation
- *LeetCode 31. Next Permutation
- 31. Next Permutation
- Leetcode 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- leetcode 31. Next Permutation
- 31. Next Permutation LeetCode
- 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- SQL server 学院考勤系统设计
- 【caffe源码研究】第三章:源码篇(12) :激活函数层
- 关于Java中枚举Enum的深入剖析
- 用sql server2005(或Excel+sql server2005)+sqlserver2sqlite_converter工具,自己写db文件方法
- 230 atom-beautify 安装 php-cs-fixer 和 phpcbf
- 31. Next Permutation
- 拓扑排序(算法模板)
- 网络程序设计
- opnet之加强mm1模型之删除事件
- Linux命令记录
- 基于Android的mvc、mvp以及mvvm架构分析(上)
- 【caffe源码研究】第三章:源码篇(13) :损失层
- android studio 多渠道打包以及工程的定制化(上)
- 33. Search in Rotated Sorted Array 旋转排序数组极值的二分求法