Next Permutation - LeetCode
题目链接
注意点
- 如果是字典序最大的串则要返回字典序最小的串
解法
解法一:参见:http://www.cnblogs.com/grandyang/p/4428207.html 时间复杂度O(n)。
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n = nums.size(),i = n-2,j = n-1;
while(i >= 0 && nums[i] >= nums[i+1]) i--;
cout << i;
if(i >= 0)
{
while(nums[j] <= nums[i]) j--;
swap(nums[i],nums[j]);
}
reverse(nums.begin()+i+1,nums.end());
}
};

小结
- 排列题,有许多变种
Next Permutation - LeetCode的更多相关文章
- Next Permutation leetcode java
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- Palindrome Permutation -- LeetCode
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Permutations II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- TPO-19 C2 Cafeteria's Food Policy
TPO-19 C2 Cafeteria's Food Policy 第 1 段 1.Listen to a conversation between a student and the directo ...
- 关于IPointerEnterHandler和IpointerExitHandler的简单说明
自己在学习UGUI背包系统时用到了,而且遇到一些问题,所以在这里写一下这两个接口的简单说明. 继承IPointerEnterHandler和IpointerExitHandler这两个接口后可以实现两 ...
- 011 --Mysql中特定查询
1.优化COUNT()查询 COUNT()可能是被大家误解最多的函数了,它有两种不同的作用,其一是统计某个列值的数量,其二是统计行数.统计列值时,要求列值是非空的,它不会统计NULL.如果确认括号中的 ...
- windows离线补丁包下载路径-云盾安骑士
我们有一台阿里云服务器,只有内网可以访问,无外网资源. 阿里云账户邮箱反复收到阿里云云盾的漏洞报警通知,推荐安装安骑士组件. 安骑士组件是收费服务.阿里云服务器,无法连上外网,无法自动安装补丁.需要将 ...
- MyCat安装与测试教程 超详细!
MyCat安装与测试教程 超详细! MyCat基础知识 一.什么是MYCAT? 1. 一个彻底开源的,面向企业应用开发的大数据库集群 2. 支持事务.ACID.可以替代MySQL的加强版数据库 3. ...
- MyBatis学习(一)————纯jdbc编程
什么是JDBC JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java ...
- bing词典
一.bug寻找 bug1:点击单词挑战之后选择四级词汇,然后一直狂击答案,点到一个时候就会出现一个情况:不管点击哪一个选项都不会跳至下一题,而且屏幕上方的已做题目数 x/20中的x会乱跳. bug2: ...
- Linux基础入门--01~03
- 一个web项目中间的团队管理
一个web项目中间的团队管理 最近在参加一个比赛,我们选的题目是:MOOC大型网络在线课堂.这个题目是我们五个人都想做的,我们的成员都是志同道合的五个人. 作为团队的统率者: 定义规范 ...
- eclipse异常关闭,而Tomcat然在运行解决方法
1.eclipse异常关闭,而Tomcat然在运行,再启动tomcat会出现端口冲突 解决方法:打开任务管理器,找到javaw.exe,点击关闭,就可以了