[Leetcode]Next Permutation
来源:互联网 发布:手机变声软件女变男 编辑:程序博客网 时间:2024/06/05 05:43
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> &num) {vector<int>::iterator it = num.end() - 1;while (it != num.begin()){if (*(it) > *(it - 1)) break;it--;}if (it != num.begin()){it--;vector<int>::iterator tmp = num.end() - 1;while (*(tmp) <= *(it)) tmp--;swap(*(tmp), *(it));reverse(it + 1, num.end());}else{reverse(num.begin(), num.end());}}};
0 0
- LeetCode: Next Permutation
- LeetCode Next Permutation
- LeetCode : Next Permutation
- [Leetcode] Next Permutation
- [LeetCode] Next Permutation
- [Leetcode] Next Permutation
- leetcode 88: Next Permutation
- [LeetCode]Next Permutation
- LeetCode-Next Permutation
- [leetcode]Next Permutation
- LeetCode - Next Permutation
- LeetCode 31: Next Permutation
- 【leetcode】Next Permutation
- LeetCode题解:Next Permutation
- 【LeetCode】Next Permutation
- [LeetCode] Next Permutation
- LeetCode Next Permutation
- leetCode | Next Permutation
- 汇编指令b和bl区别
- hackerrank_Permutation game
- Extjs4.2进阶--完整前后台交互带数据库(带源码)
- javascript获取URL参数
- iOS linker command failed with exit code 1 (use -v to see invocation)错误
- [Leetcode]Next Permutation
- [C++]LeetCode: 34 Combination Sum II
- 就从今天开始吧
- 数据采集系统(数据调查)学习总结(四)
- 离散对数计算的Sherwood 算法
- android格式化日期与时间戳学习
- River的Android学习笔记——数据持久化技术(SharedPreference存储)
- SDUTOJ 2498 AOE网上的关键路径(最长路)
- Hibernate映射解析——关联映射(一)