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. PC-IIS因为端口问题报错的解决方法

    1.我的电脑-管理-服务和应用程序-Internet信息服务  情况:这时发现“默认 SMTP 虚拟服务器”停止  解决方法:右击启动  情况:发现网页还是打不开.2.Internet信息服务-网站- ...

  2. 从app里跳到appstore评论页面的实现

    // 如果要实现在应用里面跳到appstore的对应评论页面里面的话,只要将下面地址中App_ID替换成自己的id就可以了,其他的地方都不用管. // 如果要用Safari浏览器做实验的话可以将地址中 ...

  3. Android UI开发第三十四篇——SlidingPaneLayout

    SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...

  4. java_list<String> string[]拼接json

    private String getJsonStr(List<String> jsonKeyList, String[] values){ String jsonStr = "{ ...

  5. java 泛型中 T、E ... 和 问号(通配符)的区别

    一.泛型中T.E ...  是泛型类.泛型方法定义时候用的. 1.泛型类定义在类后面 紧跟类名后面 public class TestClassDefine<T>{} 2.泛型方法定义在方 ...

  6. jenkens构建脚本

    Build   Root POM   Goals and options Command # consts SERVER="192.168.60.209" DEPLOY=" ...

  7. 使用dispatch_once:创建单列

    无论是爱还是恨,你都需要单例.实际上每个iOS或Mac OS应用都至少会有UIApplication或NSApplication. 什么是单例呢?Wikipedia是如此定义的: 在软件工程中,单例是 ...

  8. 【转】cocos2d-x 开发中使用的一些工具

    http://k57box.blog.163.com/blog/static/142261374201327112050361/ 这些工具平常也用到,不过没有像这样整理出来,这是我在网上看到的.就记录 ...

  9. 关于IB_DESIGNABLE / IBInspectable的那些事

    前言 IB_DESIGNABLE / IBInspectable 这两个关键字是在WWDC 2014年”What’s New in Interface Builder”这个Session里面,用Swi ...

  10. C#异常之(已有打开的与此 Command 相关联,已有打开的与此命令相关联的 DataReader,必须首先将它关闭。)

    异常提示:“System.InvalidOperationException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理  其他信息: 已有打开的与此 Comman ...