Next Permutation


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,31,3,2
3,2,11,2,3
1,1,51,5,1

 1 class Solution {
 2 public:
 3     void nextPermutation(vector<int>& nums) {
 4         int i,j,len=nums.size();
 5         for(i=len-2;i>=0&&nums[i+1]<=nums[i];--i)
 6         ;
 7         if(i<0)
 8         {
 9             reverse(nums.begin(),nums.end());
10             return;
11         }
12         for(j=len-1;nums[j]<=nums[i];j--)
13         ;
14         swap(nums[i],nums[j]);
15         reverse(nums.begin()+i+1,nums.end());
16         return;
17         
18     }
19 };

优质内容筛选与推荐>>
1、完全二叉树的构建及三种遍历
2、unity5, animator state machine, 无条件transition实现播放动画序列
3、百度硬盘可以检索的字节测试
4、2000 和2003下开3389命令 和注册表文件
5、Docker(一):什么是docker


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号