Next Permutation

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

解法一:just a joke :)

class Solution {
public:
void nextPermutation(vector<int> &num) {
next_permutation(num.begin(), num.end());
}
};

解法二:

1、如果数组为降序,则根据题意,升序排序后返回。

2、如果数组为升序,则交换最后两个元素后返回。

3、举例来说明:

[1,2,5,4,3]-->[1,3,2,4,5]

从右往左递增的序列是不改动的。因为递增已经是最大。

因此要改动的就是递增部分的断点。

如上例,5,4,3是不可能改动的,越改越小,与“下一个序列”的定义不符。

因此要改动的就是2.

需要将2的位置替换为3,也就是从右往左比2大的数中最小的那个数,也就是3。如果不是选最小,那就会跳过很多排列。

将3放到2的位置,新排列的头部为[1,3]。

由于3第一次出现在第二个位置,因此其余数组应该呈最小序,也就是将[5,4,2]排序。

最终结果为[1,3,2,4,5]

class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.empty())
return;
int size = nums.size();
int ind = size-;
while(ind > && nums[ind] <= nums[ind-])
ind --;
if(ind == )
{// descend
sort(nums.begin(), nums.end());
}
else if(ind == size-)
{// ascend
// swap the last two element
swap(nums[ind], nums[ind-]);
}
else
{
int ind2 = size-;
while(nums[ind-] >= nums[ind2])
ind2 --;
// nums[ind-1] < nums[ind2]
swap(nums[ind-], nums[ind2]);
sort(nums.begin()+ind, nums.end());
}
}
};

【LeetCode】31. Next Permutation (2 solutions)的更多相关文章

  1. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  2. 【LeetCode】31. Next Permutation

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

  3. 【一天一道LeetCode】#31. Next Permutation

    一天一道LeetCode系列 (一)题目 Implement next permutation, which rearranges numbers into the lexicographically ...

  4. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  5. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  6. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  7. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  8. 【LeetCode】031. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  9. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...

随机推荐

  1. Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)

    导航抽屉(navigationdrawer)是一个从屏幕左边滑入的面板,用于显示应用的主要导航项目.用户能够通过在屏幕左边缘滑入或者触摸操作栏的应用图标打开导航抽屉. 导航抽屉覆盖在内容之上,但不覆盖 ...

  2. 用java在客户端读取mongodb中的数据并发送至服务器

    使用Java自带的socket端口来实现,程序如下: Client.java package com.cn.gao; import java.net.*; import java.io.*; impo ...

  3. 把Scala代码当作脚本运行

    1. 在类UNIX系统上作为脚本运行 在类Unix系统上,你可以设置一个shell前导词来执行脚本.如下例: Script.scala #!/usr/bin/env scala !# println( ...

  4. Depth of field --Circle of confusion 推导

    https://en.wikipedia.org/wiki/Circle_of_confusion https://developer.download.nvidia.com/books/HTML/g ...

  5. 告诉我, 究竟我的SQL Server慢在哪里?

    你可以使用下面的语句来使用sys.dm_os_wait_stats这个DMV得到线程的等待信息(线程在等什么, 等了多久)的统计数值. WITH [Waits] AS (SELECT [wait_ty ...

  6. Insert Interval leetcode java

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  7. 开源 java CMS - FreeCMS2.2 网站管理

    项目地址:http://www.freeteam.cn/ 网站管理 FreeCMS支持站点群模式,并支持无限树级管理. 网站的相关文件在site文件夹下,每一个网站有自己的文件夹,源文件文件夹名就是自 ...

  8. iframe之onload事件小记

    项目上做了一个具有wizard(向导)功能的菜单导航页面,子页面的引入通过主页面上iframe的src属性切换实现.为了有个良好的交互体验,每次更新iframe的src时,主页面上都显示一个模态的lo ...

  9. [转] 公司局域网中代码访问 github.com

    一. 文档清晰 tencent同事.推荐 https://www.chenyudong.com/archives/use-git-or-github-in-company-local-net.html

  10. Cocos Studio is EOL'd

    Cocos Studio is EOL'd Cocos Studio has been EOL'd as of April 2016. There will be no more releases o ...