leetcode Next Permutation
来源:互联网 发布:公众号小说收费源码 编辑:程序博客网 时间:2024/06/06 08:46
参考网上思想,http://blog.csdn.net/luofengmacheng/article/details/16818045
从后面往前面遍历,假设当前比较的后面的值是a,前面的值是b,如果b>=a,则继续遍历,如果b<a,就从a开始,到数列尾部,选取一个比b大的最小的整数,将这个整数删除,然后放到b的前面,最后将该值后面的整数从小到大排列即可。最后,如果一直是b>=a,则重新按照升序排列
。
class Solution {public: void nextPermutation(vector<int>&num){ int tmp = 0; vector<int>::iterator it1, it2; for(it1 = num.end()-1; it1 != num.begin(); it1--){ if(*it1 > *(it1-1)){ it2 = it1; while(it2 != num.end()&& *it2 > *(it1-1)) ++it2; --it2; tmp = *it2; num.erase(it2); it2 = num.insert(it1-1,tmp); sort(it2+1,num.end()); break; } } if(it1 == num.begin()) sort(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
- GetObject
- 4.1函数定义和调用
- 【iOS】Autosynthesized property警告解决方案
- Android中listview内容较多时拖动不流畅问题
- 【Watir Webdriver】自动化上传照片到百度相册
- leetcode Next Permutation
- 混淆的艺术-(苍井空变凤姐)Proguard源码分析(三)Proguard配置解析~上
- 添加文件到Sdcard出现Failed to push selection: Invalid argument问题
- 4.2 函数形参、实参、默认参数
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
- UITableView 滚动流程性优化
- 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
- PowerDesigner导出mysql脚本里包含视图
- macbook装机注意事项&&双系统崩溃解决办法