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);
) {
int right = findMinGreater(v, left);
swap(v[left], v[right]);
}
reverse(v.begin() + left + , v.end());
}

private:
int findIncFromRight(vector<int> &v) {
int i;
; i > ; --i) {
] < v[i])
break;
}
;
}

int findMinGreater(vector<int>& v, int left) {
int i;
; i < v.size(); ++i) {
if (v[left] >= v[i])
break;
}
;
}
};```

## 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】

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

4. 【LeetCode】31. Next Permutation

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

5. LeetCode OJ 31. Next Permutation

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

6. LeetCode - 31. Next Permutation

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

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

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

8. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

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

9. LeetCode 31. Next Permutation （下一个排列）

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

## 随机推荐

1. 操作系统核心原理-7.设备管理：I/O原理

一.I/O的基本知识 1.1 为何要有I/O 前面阐述了操作系统具有进程管理.内存管理.外存管理三大核心功能,但是计算机归根是为人类服务的,这就要求计算机必须提供某种机制使得人们可以向计算机发出命令或 ...

2. 第二十八篇：SOUI中自定义控件开发过程

在SOUI中已经提供了大部分常用的控件,但是内置控件不可能满足用户的所有要求,因此一个真实的应用少不得还要做一些自定义控件. 学习一个新东西,最简单的办法就是依葫芦画瓢.事实上在SOUI系统中内置控件 ...

3. Visual Studio+TFS--强大的项目管理工具

一.前言 微软的Visual Studio非常强大,可以无缝结合Git或自家的TFS(Team Foundation Server),进行项目管理非常方便,从需求分析.开发.测试.维护,几乎可以贯穿软 ...

4. [LeetCode OJ] Max Points on a Line

Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

5. Centos7 安装MPlayer过程详解

使用自带的totem基本上都无法播放视频,各种格式都不支持,令人无语.想到了MPlayer,为了看片,决定编译安装,过程真的是折腾.如图是自带的Totem播放提示,安装了解码还是无法播放,反正要找其他 ...

6. 七牛云存储Python SDK使用教程 - 上传策略详解

文 七牛云存储Python SDK使用教程 - 上传策略详解 七牛云存储 python-sdk 七牛云存储教程 jemygraw 2015年01月04日发布 推荐 1 推荐 收藏 2 收藏,2.7k  ...

/// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.WechatDocuments model) { S ...