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之间的元素进行排序

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int end = nums.size()-1;
while( end > 0 ){
if( nums[end] > nums[end-1] ){
break;
}
else{
end--;
}
}
if( end == 0 ){
sort(nums.begin(),nums.end());
}
else{
int min = nums[end];
int index = end;
for( int i = nums.size()-1; i > end; i-- ){
if( nums[i] < min && nums[i] > nums[end-1] ){
min = nums[i];
index = i;
}
}
swap(nums[index],nums[end-1]);
sort(nums.begin()+end,nums.end());
}
}
};

  

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. 跨域资源共享(CORS)问题解决方案

    CORS:Cross-Origin Resource Sharing(跨域资源共享) CORS被浏览器支持的版本情况如下:Chrome 3+.IE 8+.Firefox 3.5+.Opera 12+. ...

  2. 使用Mulesoft建立webservice, simple方式,POJO

    Mulesoft是使用CXF来支持web service,有三种方式 1.JAX-WS 2.Simple, POJO 3. Proxy, pass-throught 本文介绍POJO,最简单的方式 1 ...

  3. AmazeUI定制

    定制流程 下载 Amaze UI 源码:从 GitHub 选择版本,点击 Source code (zip) 下载并解压(定制只适用于 Amaze UI 2.x): 下载安装 Node.js: 全局安 ...

  4. 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径1

    import java.util.ArrayList; import java.util.List; // 模块E public class AdjMatrixGraph<E> { pro ...

  5. iOS开发:正则表达式

    语法/字符说明表 值表达 .      匹配除换行符外的任意字符 \w   匹配字母或者数字的字符 \W   匹配任意不是字母或数字的字符 \s    匹配任意的空白符(空格.制表符.换行符) \S ...

  6. EntityFrameWork使用过程问题总结

    1.记录上次遇到个一个问题. (1).vs2013中的EntityFramework不能识别odp11,所以在用ef的时候 ,要换vs2012 (2).opd12不能识别Oracle  9i(所以这个 ...

  7. CSS过滤器

    CSS过滤器(CSS filters)最初是为了向SVG矢量图提供不同的图片效果.现在,CSS filters 不在局限于在SVG中使用,也可以在图片.文字和其它元素上使用.CSS过滤器效果并不难理解 ...

  8. 【其他】win7创建wifi热点共享给手机使用

    出门在外,有时候网络有诸多不便,需要用笔记本创建wifi热点给手机用:本人测试xp怎么配置都不好使,但win7有可行的方案,不依赖第三方软件. 详述如下: 场景一:win7 + A(PC机)(用无线连 ...

  9. centos7 系统初始化脚本

    现在自己的本地虚拟机系统,直接安装的是centos7.2 mini版,安装完成发现好多东西都没有安装,所以写了一个简单的系统初始化脚本,让自己可以省一些力气,哈哈 人懒主要是. 下面贴出写的脚本,脚本 ...

  10. canvas转盘抽奖

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" ...