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. jQuery回溯

    1.jQuery有个很好用的方法是 end(); 2.在进行链式操作时,使用end方法,可以回溯到上一个jQuery对象. 3.实现原理: jQuery内部有一个对象栈,当形成新的对象时,会将新对象推 ...

  2. JSP自定义标签开发入门《转》

    JSP自定义标签开发入门 一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; ...

  3. 【BZOJ1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 后缀数组

    [BZOJ1717][Usaco2006 Dec]Milk Patterns Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量 ...

  4. org.apache.commons.beanutils.BeanUtils的常见用法

    import org.apache.commons.beanutils.BeanUtils BeanUtils1. public static void copyProperty(Object bea ...

  5. 从零打造在线网盘系统之Hibernate查询与更新技术

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

  6. 【css预处理器】------css预处理器及sass基本介绍------【巷子】

    001.什么是css预处理器? css预处理器定义了一种新的语言.用一种专门的编程语言,为css增加了一些编程的特性,将css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作.(通俗点说“” ...

  7. 如何在window的location使用target

    在页面中window的location跳转时,指定页面在框架中跳转 1. 如果你要让最顶层的框架跳转,就是整个页面,相当于用traget指向顶层 window.top.location   =   & ...

  8. ASIOTutorial

    ASIOTutorialTutorial 1: Simple wgetTutorial 2: Synchronous HTTP ServerTutorial 3: Asynchrnous wgetTu ...

  9. ROS 命令学习记录

    #catkin_init_workspace //just get CMakeList.txt #catkin_make  //增加了信文件夹 build和devel #Catkin_creat_pk ...

  10. Openstack(二)基本环境准备--网络、时间、yum源等

    2.1服务器版本安装 2.1.1服务器使用:centos7.4 + vm12 2.1.2重命名网卡: 传递内核参数 net.ifnames=0 biosdevname=0,以更改网卡名称为eth0,e ...