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. 通过分析 JDK 源代码研究 Hash 存储机制

    通过 HashMap.HashSet 的源代码分析其 Hash 存储机制 实际上,HashSet 和 HashMap 之间有很多相似之处,对于 HashSet 而言,系统采用 Hash 算法决定集合元 ...

  2. hadoop-2.6.0.tar.gz + spark-1.5.2-bin-hadoop2.6.tgz 的集群搭建(3节点和5节点皆适用)

    本人呕心沥血所写,经过好一段时间反复锤炼和整理修改.感谢所参考的博友们!同时,欢迎前来查阅赏脸的博友们收藏和转载,附上本人的链接.http://www.cnblogs.com/zlslch/p/584 ...

  3. 教程-Delphi资源文件(全面分析于使用)

    Delphi资源文件(全面分析之位图.光标.图标.AVI.JPEG.Wave)   几乎每个Windows应用程序都使用图标.图片.光标等资源.资源是程序的一部分,但是它是不可执行代码.下面我们就详细 ...

  4. SQL注入专题

    SQL注入专题--整理帖 SQL注入是从正常的WWW端口访问,而且表面看起来跟一般的Web页面访问没什么区别, 所以目前市面的防火墙都不会对SQL注入发出警报,如果管理员没查看IIS日志的习惯,可能被 ...

  5. 树莓派(Rospberry Pi B+)到货亲測

    1 图鉴 Rospberry Pi  B+最终在今天下午有蜗牛快递公司圆*送到了.B+主要是添加了2个USB,添加了GPIO,sd卡换成了micro sd ...先不说直接上图再说,期待了好久好久 w ...

  6. SQL Server数据库PIVOT函数的使用详解(一)

    http://database.51cto.com/art/201108/285250.htm SQL Server数据库中,PIVOT在帮助中这样描述滴:可以使用 PIVOT 和UNPIVOT 关系 ...

  7. apache+php+mysql最新版windows下

    卸载以前的Apache 1.控制面板先卸载 2.删除E:\Program Files (x86)\Apache Software Foundation下的apache目录 一.安装apache 1.c ...

  8. Android(java)学习笔记141:SQLiteDatabase的query方法参数分析

    public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] se ...

  9. base查找方法的实现JAVA

    import java.util.List; import java.util.ArrayList; import java.util.Scanner; /*在一个有序数组中查找一个数的过程,模拟二分 ...

  10. JUnit4注解基本介绍

    @After If you allocate external resources in a Before method you need to release them after the test ...