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

代码:

class Solution {
public:
void nextPermutation(vector<int> &v) {
int left = findIncFromRight(v);
if (left != -) {
int right = findMinGreater(v, left);
swap(v[left], v[right]);
}
reverse(v.begin() + left + , v.end());
} private:
int findIncFromRight(vector<int> &v) {
int i;
for (i = v.size()-; i > ; --i) {
if (v[i-] < v[i])
break;
}
return i - ;
} int findMinGreater(vector<int>& v, int left) {
int i;
for (i = left+; i < v.size(); ++i) {
if (v[left] >= v[i])
break;
}
return i-;
}
};

leetcode problem 31 -- Next Permutation的更多相关文章

  1. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

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

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

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

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

  4. LeetCode 【31. Next Permutation】

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

  5. 【LeetCode】31. Next Permutation

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

  6. LeetCode OJ 31. Next Permutation

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

  7. 【LeetCode】31. Next Permutation (2 solutions)

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  8. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  9. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

随机推荐

  1. CentOS上安装FastDFS分布式文件系统

    鱼大自己写的项目简介:http://bbs.chinaunix.net/thread-1920470-1-1.html 架构简介:http://www.programmer.com.cn/4380/ ...

  2. ldconfig及 LD_LIBRARY_PATH

    dconfig及 LD_LIBRARY_PATH 1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library ...

  3. void,extern,sizeof

    高手潜规则:禁用goto 程序质量与goto出现次数成反比 void指针的意义 1.C语言规定只有相同类型的指针才可以相互赋值 2.void*指针作为坐值用于"接收"任意类型的指针 ...

  4. SRM659 1100pts

    绍一模拟赛的题[问题描述]小Z.小Y和小B拥有

  5. jQuer基础

    一.概述 jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多 ...

  6. CAS认证(2):认证过程

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  7. C++ ComboBox基础

    关键点 实现过程 //添加 //添加字符串 m_cbo1.AddString("AAA"); m_cbo1.AddString("BBB"); m_cbo1.A ...

  8. 高亮选中MEMO某一行

    选中第5行 //转到指定行并选中这行的文本 procedure SelectLine(Memo1: TMemo; ln: Integer); begin Memo1.SelStart := SendM ...

  9. mysql中对数据库的每个表执行优化的存储过程

    说明:此处为<高性能mysql(第二版)>中的示例代码,除了数据库名 其他未经更改.仅供学习及参考 对数据库的每个表执行优化的存储过程 CREATE PROCEDURE `inventor ...

  10. Memcached启动、关闭参数(摘录)

    启动参数注释如下: -p <num>  指定服务TCP端口,默认为11211 -U <num>   指定服务UDP端口  默认11211表示打开,设置0表示关闭 -s < ...