[leetcode刷题系列]Next Permutation
来源:互联网 发布:济南五龙潭传说知乎 编辑:程序博客网 时间:2024/06/05 04:20
应该算经典问题了。 解法就是从后往前找到第一个小于它后面的那个数。记录下他的位置p,然后再从后往前中第一个大于位置p上面的数的数的位置,记录为first。
然后交换p和first上面的数字,并将位置p以后的序列反转就可以了。
class Solution {public: void nextPermutation(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function if(num.size() <= 1) return ; int p = num.size() - 2; while(p >= 0 && num[p] >= num[p + 1]) p --; if(p < 0){ reverse(num.begin(), num.end()); return ; } for(int i = num.size() - 1; i >= p + 1; -- i) if(num[i] > num[p]){ swap(num[i], num[p]); reverse(num.begin() + p + 1, num.end()); return; } }};
- [leetcode刷题系列]Next Permutation
- leetcode刷题系列C++-next permutation
- LeetCode刷题笔录Next Permutation
- leetcode系列(45)Next Permutation
- LeetCode算法题之Next Permutation
- LeetCode算法题——Next Permutation
- 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
- Code Behind vs Template Components
- C++中关于虚拟继承的一些总结
- Android仿微信气泡聊天界面设计
- 【我的程序】 Djudge
- live555源码分析----H264的数据处理
- [leetcode刷题系列]Next Permutation
- 实现RTP协议的H.264视频传输系统
- 【实用随记】JQuery事件绑定
- 设计模式六大原则(转载)
- iOS tableVIewCell的使用总结
- Effective Java 第8条:覆盖equals时要遵守通用约定
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- 学习笔记 jquery的this
- GNU是什么/GRUB