1. Question

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

2. Solution

  1. 从后往前,找第一个下降的数字nums[i] > nums[i - 1],如果没找到那么直接将数组反转即可。

  2. 再从后往前,找一个数nums[j] > nums[i - 1],交换他们两个数字,然后再将nums[i - 1]之后的数进行反转。

  3. 时间复杂度O(n)。

3. Code

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 1;
for (; i > 0; i--) { // 找第一个下降的位置
if (nums[i] > nums[i - 1])
break;
}
if (i == -1 || i == 0) {
reverse(nums.begin(), nums.end());
} else {
for (int j = nums.size() - 1; j >= 0; j--) {
if (nums[j] > nums[i - 1]) { // 找第一个比下降位置数大的
swap(nums[j], nums[i - 1]);
reverse(nums.begin() + i, nums.end()); // 反转之后的数
break;
}
}
}
}
};

LeetCode——Next Permutation的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Next Permutation 下一个排列

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

  5. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  9. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

  10. LeetCode Find Permutation

    原题链接在这里:https://leetcode.com/problems/find-permutation/description/ 题目: By now, you are given a secr ...

随机推荐

  1. 高中生的IT之路-1.3那一幕

    上一篇讲到,当时我认为自己的命运就是小时候上学,长大后外出打工,所以高中毕业后就来到了天津,到爸爸的店铺打工. 我爸的店铺就在天津大学校园里,幸运的是,我人生的转折点也就在此. 刚到店里那段时间,每天 ...

  2. android去除Spinner的分割线

    <style name="TestSpinnerStyle" parent="android:style/Widget.ListView.DropDown" ...

  3. android选择图片或拍照图片上传到服务器(包括上传参数)

    From:http://blog.csdn.net/springsky_/article/details/8213898具体上传代码: 1.选择图片和上传界面,包括上传完成和异常的回调监听 [java ...

  4. 【Android】Android 发送短信和打电话的方法

    发送短信的方法 有两种方法可以实现发送短信,其一是使用intent-startActivity,URI数据格式为"smsto:num",调用的action为Intent.ACTIO ...

  5. git add -A和git add . 的区别

    git add -A和 git add . git add -u在功能上看似很相近,但还是有所差别. git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容 ...

  6. 有关velocity的资料(等待整理)

    proxy-target-class="true" 与proxy-target-class="false"的区别: proxy-target-class属性值决 ...

  7. 从零打造在线网盘系统之Struts2框架起步

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  8. pandas的DataFrame用法

    用来生成DataFrame数据 1.说明: class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=F ...

  9. Ubuntu下MySQL主从同步配置

    一.在两台Linux机器上安装MySQL 二.Master主服务器配置(192.168.1.3) 1.编辑my.cnf编(命令查找文件位置:find / -name my.cnf) vi /etc/m ...

  10. UA-* headers

    HTTP The Definitive Guide Request headers are headers that make sense only in a request message. The ...