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. 关于JSONP以及跨域相关

    什么是跨域: 浏览器对ajax请求的限制,不允许跨域请求资源. http://www.a.com--->http://www.b.com       是跨域 http://www.a.com-- ...

  2. Java:IO流的综合用法(从键盘录入数据并打印在控制台上)

    import java.io.*; public class IOTestDouble { public static void main(String[] args)throws Exception ...

  3. Android系统file_contexts二进制与文本转换工具

    #ifdef _WIN32 #define __USE_MINGW_ANSI_STDIO 1 #endif #include <stdio.h> #include <stdlib.h ...

  4. Oracle中的数值处理方法

    求绝对值函数 ) from dual; 求平方根函数 ) from dual; 求幂函数 ,3) from dual; 求余弦三角函数 select cos(3.14159) from dual; 求 ...

  5. 2.Dynamic Programming on Stolen Values【dp】

    Problem: There are  n houses built in a line, each of which contains some value in it. A thief is go ...

  6. RHEL7系统修复rm -rf /boot /etc/fstab

    RHEL7/Centos7系统发布这么长时间了,大家都知道这个系统的一个特点就是用systemctl代替了init系统守护进程,系统越来越模块化了.在新版的系统中许多的命令也发生了改变,grub也变为 ...

  7. Oracle查询client编码集

    Oracle查询client编码集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ---------------- ...

  8. 【图解】javaScript组成结构

  9. linux 命令行选项

    命令行选项风格 1.原始unix风格     a.命令行选项以连字符'-'开头,后跟单个字符表示选项,选项后面跟着取值,如:mysql -hlocalhost b.选项不带取值的,可以组合在一起,如: ...

  10. iptables日志与limit参数

    在处理工作问题的时候需要查看防火墙的日志,由于默认日志都是在系统日志里/var/log/messages里面.需要对rsyslog做设置. 首先编辑配置文件/etc/rsyslog.conf如下: # ...