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.找到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 【47. Permutations II】

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

6. LeetCode【217. Contains Duplicate】

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

7. LeetCode【169. Majority Element】

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

8. LeetCode【第217题】Contains Duplicate

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

9. [array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

## 随机推荐

1. JavaWeb:EL表达式

JavaWeb:EL表达式 说明 1.Expression Language语言,一种用于JSP文件中的数据访问的语言. 2.能够简化JSP文件中该数据访问的代码,可用来替代传统的基于<%= % ...

2. 深入JavaScript：词法分析、连续赋值猜想

JavaScript:词法分析.连续赋值猜想 原创文章,转摘请注明出处:苏福:http://www.cnblogs.com/susufufu/p/5851642.html 深夜发文,先吐槽下博客园的编 ...

3. Euclidean Space

http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

4. Log4j配置与使用

log4j是Java社区事实上的日志标准解决方案.使用起来比较简单. 一. 简单使用 1.下载jar包放到lib文件夹,并加入到build path中: 2.编写log4j.properties文件, ...

5. 浅析Java异常

1.什么是异常 结构不佳的代码不能运行,这是Java的基本理念. 发现错误的理想时机是在编译期.然而,编译器并不能发现所有的错误,余下的问题就需要在程序运行时解决.这就需要错误能通过某种方式,把适当的 ...

6. 阅读layim代码小记，实现可以更改用户签名的方法

用layim原版的时候发现,用户的签名是不能直接修改的,如下: 其实要改也很简单,查看一下源代码,然后加一个input进去,可是,加完之后是这样的: 没关系,给它一个样式,让它乖乖的“隐藏“起来. / ...

7. Colossal Fibonacci Numbers（巨大的斐波那契数）UVA 11582

评测地址:http://acm.hust.edu.cn/vjudge/problem/41990 The i'th Fibonacci number f (i) is recursively de n ...

8. 获取View的高度宽度，屏幕参数，状态栏高度

基础 各区域示例,注意绿色,紫色,橙色区域 注意: 在onCreate只是对象的初始创建过程,这时并没有draw,这时view.getHeight返回0,可在onStart里用getHeight等. ...

9. [swustoj 917] K-lucky-number

K-lucky-number(0917) 问题描述 K-lucky-number is defined as add up the number of each bit is a multiple o ...

10. MS Writer Code Test

#include<iostream> using namepspace std; int main() { cout<<"Hello World"<& ...