leetcode第30题--Next Permutation
problem:
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> &num) {
if (num.size() == )
return;
int ind = -;
for (int i = num.size() - ; i > ; i--) // 是大于零或者大于等于1
{
if (num[i - ] < num[i])
{ind = i -;break;}
}
if (ind == -)
{sort(num.begin(),num.end());return;}
int min = INT_MAX, sw = -;
for (int j = num.size() -; j > ind; j--)
{
if(num[j] > num[ind] && num[j] < min)
{ min = num[j]; sw = j;}
}
int tmp = num[ind];
num[ind] = num[sw];
num[sw] = tmp;
vector<int>::iterator it = num.begin();
for(int i = ; i < ind + ; i++)
{
it++;
}
sort(it, num.end());
return;
}
};
这题考察什么是词典序的下一个排列。
leetcode第30题--Next Permutation的更多相关文章
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- leetcode第30题:括号生成
这是目前遇到最难的题,刚开始的思路是:匹配words中元素是否在s中,若在找所在元素的后words长度位的字符串,判断words其他元素是否都在s中. 看似这个思路可行,实际上存在的问题: 1.wor ...
- 乘风破浪:LeetCode真题_031_Next Permutation
乘风破浪:LeetCode真题_031_Next Permutation 一.前言 这是一道经典的题目,我们实在想不出最好的方法,只能按照已有的方法来解决,同时我们也应该思考一下为什么要这样做?是怎么 ...
- leetcode top-100-liked-questions刷题总结
一.起因 宅在家中,不知该做点什么.没有很好的想法,自己一直想提升技能,语言基础自不必言,数据结构还算熟悉,算法能力一般.于是乎,就去刷一通题. 刷题平台有很多,我选择了在leetcode进行刷题.回 ...
- 【python】Leetcode每日一题-扰乱字符串
[python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...
- 【python】Leetcode每日一题-最大数
[python]Leetcode每日一题-最大数 [题目描述] 给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意:输出结果可能非常大,所以你需要返回一个 ...
- 【python】Leetcode每日一题-丑数
[python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
随机推荐
- 2012在数据库技术会议上的讲话PPT打包
2012技术大会演讲PPT打包 DB2 Overview of Disaster Recovery Options.pdf: http://www.t00y.com/file/76767890 ...
- SQL开发中容易忽视的一些小地方(五)
原文:SQL开发中容易忽视的一些小地方(五) 背景: 索引分类:众所周知,索引分为聚集索引和非聚集索引. 索引优点:加速数据查询. 问题:然而我们真的清楚索引的应用吗?你写的查询语句是否能充分应用上索 ...
- hdu 3911 Black And White(线段树)
题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...
- Boost.Asio c++ 网络编程翻译(26)
Boost.Asio-其他特性 这章我们讲了解一些Boost.Asio不那么为人所知的特性.标准的stream和streambuf对象有时候会更难用一些,但正如你所见.它们也有它们的益处.最后,你会看 ...
- Google调试技巧总结
工欲善其事 工欲善其事,必先利器. Google调试面板一一介绍:F12回想一下大家都应该知道,哈哈 element面板 这个面板显示了页面所有html代码.用于调试css代码.右側展示左側相应选择元 ...
- NVCC编译器
http://blog.csdn.net/bendanban/article/details/8518382 mark一下 几个方案可以用: 方案1: 将所有文件分别编译,最后统一合并! 对于C程序 ...
- (大数据工程师学习路径)第三步 Git Community Book----Git基本用法(上)
一.git的初始化 1.Git 配置 使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名. $ git config --global user.name &quo ...
- U盘启动盘安装Win7/9/10系统攻略
UltraISO制作U盘启动盘安装Win7/9/10系统攻略 U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便,随时都可以制作系统启动盘. U盘建议选择8G ...
- PHP 模板方法模式使用
模板方法模式 用于各个子类均需实现类似的步骤,但是在这些步骤过程中,有各个子类不同的实现方法,也有他们公共的实现方法. 示例代码: //==================== //模板方法模式 // ...
- 【iOS发展-81】setNeedsDisplay刷新显卡,并CADisplayLink它用来模拟计时器效果
(1)效果 (2)源码下载(假设提示没有小图片的话,自己找一个替换一下即可,看到效果即可) http://download.csdn.net/detail/wsb200514/8176339 (3)总 ...