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. mysql in查询排序问题

    SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 这样的情况取出来后,其实,id还是按1,2,3,4,5,6,7,8,9,排序的,但如果我 ...

  2. CVE-2018-2628 weblogic WLS反序列化漏洞--RCE学习笔记

    weblogic WLS 反序列化漏洞学习 鸣谢 感谢POC和分析文档的作者-绿盟大佬=>liaoxinxi:感谢群内各位大佬及时传播了分析文档,我才有幸能看到. 漏洞简介 漏洞威胁:RCE-- ...

  3. LINUX IPTABLES 防火墙配置

     0.iptables(ACL)的匹配原则: 与cisco等一致,从上到下依次匹配. 1.iptables的基本用法:. (1)命令格式 iptables [–ttable] command [mat ...

  4. nodejs 环境搭建

    一 下载nodejs 官网:http://nodejs.cn/ 有时官网有点慢,可以去其他地方下载 统一下载站:http://www.3987.com/xiazai/2/43/57188.html 二 ...

  5. 【BZOJ3677】[Apio2014]连珠线 换根DP

    [BZOJ3677][Apio2014]连珠线 Description 在列奥纳多·达·芬奇时期,有一个流行的童年游戏,叫做“连珠线”.不出所料,玩这个游戏只需要珠子和线,珠子从1到礼编号,线分为红色 ...

  6. 补课:PageRank

    最近连续听到PageRank算法,久闻其名,不闻其详,心里虚得很,今儿补补课. PageRank算法的网络资料非常全面,毕竟是将近二十年的经典算法,算法细节可以参考文末链接,这里简单说说我的理解. P ...

  7. Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法

    一.需求背景     因此次项目的生产环境中部署了多套系统,每天会产生大量的日志(数百GB的量),侵占了服务器宝贵的存储资源空间.为了有效缓解服务器存储压力,考虑通过Linux的Shell脚本结合cr ...

  8. mysql GROUP_CONCAT 用法

    group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) 基本查询 mysql> select * f ...

  9. [EF]vs15+ef6+mysql这个问题,你遇到过么?

    写在前面 因为最近用mysql比较多,所以想了解下ef+mysql的内容,发现ef连接mysql数据库,还有那么一段路折腾.折腾到最后,发疯了. 步骤 这里采用db first的方式来使用ef. 通过 ...

  10. StartUML-时序图