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. C语言条件编译

    使用与平台有关的C语言函数,可能会使得程序不具有可移植性.比如Socket编程.多线程编程等是与平台有关的. 若想将程序做成平台无关的就需要用到与平台相关的条件编译. 下面转自:http://blog ...

  2. 国内各视频网站android pad客户端支持分辨率情况初步统计

    视频网站名称 800*600 1024*600 1280*800 其他 国际化   备注 优酷 支持 支持 支持 支持 不支持     土豆网 没有pad版的 没有pad版的 没有pad版的 支持 不 ...

  3. IIS-设置session时间

    session会话类型

  4. IE8中伪元素动态作用样式不重绘bug记录

    前阵子对公司框架的前端优化中,使用了字体图标(iconfont)来做模块的图标集,供用户进行配置选择. 字体图标的有非常好的灵活性和复用性,可以像处理文字一样通过font-size进行大小设置.通过c ...

  5. Cognos定时刷新RS解决方案

    为了了解医院运营情况,在门诊和挂号业务上用户提出了一个接近实时监测的需求,每隔一段时间需要知道当天的挂号量,等待人数,出诊医生等指标来了解医院当天主要科室的就诊情况,以及医疗资源是否分配得当 一:方案 ...

  6. Cognos开发自定义排序规则的报表和自定义排名报表

    场景:有一个简单的销售数据分析,可以按照日期,按照商品类型来分析订单笔数和订单金额. 目的:用户可以自定义查看按照不同指标排序的数据,用户可以查看按照不同指标排名的前N名数据 一:功能及效果展示 效果 ...

  7. C++的四种初始化形式以及类型转换

      C++中有如下的方式来初始化一个变量. 但当进行类型转换时,只有两种方式可用,其他两种方式会报错.

  8. HTTP请求格式和HTTP响应格式

    主要内容: 1.HTTP请求格式 2.HTTP响应格式 一.HTTP请求格式 当浏览器向Web服务器发出请求时,它向服务器传递了一个数据块,也就是请求信息,HTTP请求信息由3部分组成:l   请求方 ...

  9. Android Environment.getExternalStorageDirectory() 获取的是内部存储还是外部存储?

    这几天在做Android应用的远程更新功能,将下载的更新包放在移动设备上指定的文件夹. 用的是  Environment.getExternalStorageDirectory() 这种方法.然后在获 ...

  10. Asp.Net 之 <%%>相关内联代码块用法

    1.<%@ ... %> 用来添加命名空间引用,如:<%@ import namespace="system.data"> 2.<% ... %> ...