leetcode problem 31 -- Next Permutation
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,23,2,1 → 1,2,31,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的更多相关文章
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- 【一天一道LeetCode】#31. Next Permutation
一天一道LeetCode系列 (一)题目 Implement next permutation, which rearranges numbers into the lexicographically ...
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode】31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode OJ 31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【LeetCode】31. Next Permutation (2 solutions)
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
随机推荐
- Yii目录树扩展ztree,ctree等
ztree: http://blog.csdn.net/jake451/article/details/7091449 http://hi.baidu.com/qiangtan/item/e85c48 ...
- NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"
原文: http://stackoverflow.com/questions/19874935/afnetworking-2-0-post-issue-cocoa-error-3840json-tex ...
- 包含块、层叠上下文、BFC
包含块 什么是包含块?简单来说,就是决定一个元素大小和定位的元素.一个元素会为它的内部元素创建包含块,但也不能说元素的包含块就是它的父元素: 1.position:fixed 的元素 包含块是当前可视 ...
- mmsql 查询每个分类的前3条数据
select * from (select *,row_number() over(partition by PropertyRoofBeamID order by PropertyRoomID de ...
- MySQL 统计信息
200 ? "200px" : this.width)!important;} --> 介绍 数据库维护统计信息的目的主要是为了优化器进行更好的执行优化,首先统计信息是建立在 ...
- 2015AppStore 上传步骤及常见问题
——————辛苦手写,转载请注明出处!—————— *************华丽分割线***************** 一.注意开发者账号:注意格式不能有一点错. 二.下载证书:生成描 ...
- android httpClient 支持HTTPS的2种处理方式
摘自: http://www.kankanews.com/ICkengine/archives/9634.shtml 项目中Android https或http请求地址重定向为HTTPS的地址,相信很 ...
- 部署服务--NLB
通过服务部署NLB,是对某一层(一层下面可以自定义VM实例数量)服务下的多台VM进行NLB,而不是对多个层进行NLB.需要先进行如下准备: 1.VM需要使用静态IP和静态MAC,所以需要先在进行NLB ...
- Design Of A Modern Cache
http://highscalability.com/blog/2016/1/25/design-of-a-modern-cache.html MONDAY, JANUARY 25, 2016 AT ...
- linux 清空文件内容命令
清空文件内容命令 $ echo "" >log.log > 是重写,覆盖式 >>是尾部追加