LeetCode--Next Permutation
来源:互联网 发布:淘宝里的天猫店 编辑:程序博客网 时间:2024/06/13 23:23
#include <iostream>#include <vector>#include <algorithm>using namespace std;class Solution {public:void NextPermutation(int num[], int n);};void Solution::NextPermutation(int num[], int n){<span style="white-space:pre"></span>int change = -1;<span style="white-space:pre"></span>int tmp;<span style="white-space:pre"></span>int i;<span style="white-space:pre"></span>i = n-1;<span style="white-space:pre"></span>//找到第一个不是升序的点,从后边往前遍历<span style="white-space:pre"></span>while(i!=0 && num[i-1]>=num[i])<span style="white-space:pre"></span>i--;<span style="white-space:pre"></span>if( i == 0 )<span style="white-space:pre"></span>{<span style="white-space:pre"></span>//直接反转顺序<span style="white-space:pre"></span>for(i=0; i<(n-1)/2; i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tmp = num[i];<span style="white-space:pre"></span>num[i] = num[n-1-i];<span style="white-space:pre"></span>num[n-1-i] = tmp;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>change = i-1;<span style="white-space:pre"></span>for(i=n-1; i>change; i--)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(num[i]>num[change])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tmp = num[i];<span style="white-space:pre"></span>num[i] = num[change];<span style="white-space:pre"></span>num[change] = tmp;<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>//反转顺序<span style="white-space:pre"></span>for(i=0; i<(n-1-change)/2; i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>tmp = num[change+1+i];<span style="white-space:pre"></span>num[change+1+i] = num[n-1-i];<span style="white-space:pre"></span>num[n-1-i] = tmp;<span style="white-space:pre"></span>}}int main(){Solution s;int array[4] = {1,2,2,4};vector<int> array2;array2.push_back(1);array2.push_back(2);array2.push_back(2);array2.push_back(4);for(int j=0; j<24-1; j++){s.NextPermutation(array, 4);for(int i=0; i<4; i++)cout<<array[i]<<" ";cout<<"\t";//标准stl输出next_permutation(array2.begin(), array2.end());for(vector<int>::iterator it = array2.begin(); it!=array2.end(); it++)cout<<*it<<" ";cout<<endl;}}
算法复杂度是O(n),空间复杂度是O(1)。
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
- 欢迎使用CSDN-markdown编辑器
- 23种设计模式(15):迭代器模式
- Android measure 简单理解
- iOS开发之KVC、KVO
- V8引擎之Binding
- LeetCode--Next Permutation
- myplan
- 【张孝祥并发课程笔记】14:java5阻塞队列的应用
- 搜索推广营销五步曲之三:方案制作之如何写好创意
- 项目总结之优化游戏包大小
- 动态路由距离矢量路由协议RIP
- hdu 4288 线段树+离散化
- php 安装与配置
- Interleaving String