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

这个最开始我比较不能理解题目的意思,后来才明白表示两个排序之间没有其他的排序的意思是,比如1,2,3,4下一个排序是1,2,4,3 --> 1,3,2,4 --> 1,3,4,2 --> 1,4,2,3 -->.....

就是说第二个排序组成的连续数字,比前面的大,并且中间没有其他的组合,如果已经是4,3,2,1了,那么下一个就是1,2,3,4即回到开头。

我们来研究一下上面[1,2,3,4]的排序过程,比如比1,2,3,4大的是1,2,4,3怎么出来的呢?再看看1,3,4,2 ---> 1,4,2,3

1.找到nums[i] > nums[i-1]

2.找出i-nums.size()-1之间比nums[i-1]大的最小值,交换这个值与nums[i-1]

3.对i-1到nums.size()-1之间的元素进行排序

  1. class Solution {
  2. public:
  3. void nextPermutation(vector<int>& nums) {
  4. int end = nums.size()-1;
  5. while( end > 0 ){
  6. if( nums[end] > nums[end-1] ){
  7. break;
  8. }
  9. else{
  10. end--;
  11. }
  12. }
  13. if( end == 0 ){
  14. sort(nums.begin(),nums.end());
  15. }
  16. else{
  17. int min = nums[end];
  18. int index = end;
  19. for( int i = nums.size()-1; i > end; i-- ){
  20. if( nums[i] < min && nums[i] > nums[end-1] ){
  21. min = nums[i];
  22. index = i;
  23. }
  24. }
  25. swap(nums[index],nums[end-1]);
  26. sort(nums.begin()+end,nums.end());
  27. }
  28. }
  29. };

  

LeetCode 【31. Next Permutation】的更多相关文章

  1. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

  2. LeetCode OJ 31. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. LeetCode 【190. Reverse Bits】

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. leetcode problem 31 -- Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. LeetCode【第1题】Two Sum

    准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...

  6. LeetCode 【47. Permutations II】

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. LeetCode【217. Contains Duplicate】

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. LeetCode【169. Majority Element】

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode【第217题】Contains Duplicate

    题目: ''' Given an array of integers, find if the array contains any duplicates. Your function should ...

随机推荐

  1. Scala 编程---类和对象

    类是对象的蓝图.一旦你定义了类,你就可以用关键字new从类的蓝图里创建对象.比方说,如果给出了类的定义: class ChecksumAccumulator { // class definition ...

  2. Ubuntu下mysql-server的安装

    (1)更新 #apt-get update (2)安装 #apt-get install mysql-server 出现窗口设置"root"用户的密码为"456456&q ...

  3. OGG for DB2 z/OS 12.2版本发布

    2016-04-15 Oracle发布了GoldenGate for DB2 z/OS  12.2.0.1.2.可以从OTN或eDelivery下载,该版本是ogg for DB2 z/OS的第一个1 ...

  4. C++中的文件读取结束

    while(cin>>N>>M) { } ok???

  5. HTML 行内元素和块级元素的理解及其相互转换

    块级元素:div, p(段落), form(表单), ul(无序列表), li(列表项), ol(有序列表), dl(定义列表), hr(水平分割线), menu(菜单列表), table(表格).. ...

  6. How to upgrade Subversion on OSX

    How to upgrade Subversion on OSX http://andowebsit.es/blog/noteslog.com/post/how-to-upgrade-subversi ...

  7. 基于.NET的CAD二次开发学习笔记二:AutoCAD .NET中的对象

    1.CAD对象:一个CAD文件(DWG文件)即对应一个数据库,数据库中的所有组成部分,看的见(包括点.多段线.文字.圆等)和看不见(图层.线型.颜色等)的都是对象,数据库本身也是一个对象. 直线.圆弧 ...

  8. Guess Number Higher or Lower II--困惑

    今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/gue ...

  9. yii2.0邮箱发送

    邮件发送配置: 打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php)         'mai ...

  10. Java中的Exception

    Caused by: java.lang.IllegalArgumentException: The servlets named [XXX] and [YYY] are both mapped to ...