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 (2 solutions)

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

7. LeetCode - 31. Next Permutation

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

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

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

9. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

## 随机推荐

1. ASP.NET MVC Model验证(五)

ASP.NET MVC Model验证(五) 前言 上篇主要讲解ModelValidatorProvider 和ModelValidator两种类型的自定义实现, 然而在MVC框架中还给我们提供了其它 ...

2. Spring MVC配置

web配置 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="ht ...

3. rpm命令

RPM 安装.卸载.升级.查询和验证. RPM 安装 命令: rpm -i 文件名 如: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rp ...

4. Android 手机卫士--实现设置界面的一个条目布局结构

本文地址:http://www.cnblogs.com/wuyudong/p/5908986.html,转载请注明源地址. 本文以及后续文章,将一步步完善功能列表: 要点击九宫格中的条目,需要注册点击 ...

5. Android（java）学习笔记199：Android中补间动画（Tween Animation）

本文主要简单介绍补间动画使用代码实现, 关于使用xml实现补间动画,可以参看:自定义控件三部曲之动画篇(一)——alpha.scale.translate.rotate.set的xml属性及用法 1. ...

6. android handler机制简单介绍

我们需要了解4个类: handler:处理者,用于发送和接收信息 massage:消息.里面可以存储消息的许多信息 looper:循环泵,用于循环取出消息队列中的消息 MessageQueue(一般不 ...

7. [置顶] strcpy和memcpy的区别

strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容,还会复制字符串的结束符. 已知strcpy函数 ...