31. Next Permutation
来源:互联网 发布:sql关联表查询 编辑:程序博客网 时间:2024/06/07 07:55
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
思路1:利用STL里面的相关函数即可
void nextPermutation(vector<int>& nums) { if (nums.size() <= 1)return; next_permutation(nums.begin(), nums.end());}
思路2:将STL里面的相关函数具体实现。。。
void nextPermutation(vector<int>& nums) { if (nums.size() <= 1)return; int len = nums.size(); for (int i = len - 1; i >= 0; i--){ for (int j = len - 1; j > i; j--){ if (nums[i] < nums[j]){ swap(nums[i], nums[j]); sort(nums.begin() + i + 1, nums.end()); return; } } } sort(nums.begin(), nums.end()); return;}
阅读全文
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
- 二进制中1的个数
- Java之NIO
- windows下面安装easy_install和pip教程
- frameset标签设计页面
- java并发编程学习(3)
- 31. Next Permutation
- 关于javaScript switch 使用心得
- 代理模式(静态代理)的通俗理解
- [算法系列之十八]海量数据处理之BitMap
- Servlet 与 Ajax 交互一直报status=parsererror
- mysql的四种隔离级别
- 树莓派配置之换源
- Linux文件编程之遍历目录
- 面向对象-代码块看程序写结果