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 ...
随机推荐
- MOD 模除运算符
用于奇数和偶数的校验,星期几的计算,以及其它专门的计算.
- JDBC Mysql 驱动连接异常
在做JDBC连接Mysql的时候遇到了三个异常: 第一个是:mysql8.0 caching_sha2_password 这个异常是由于是因为在mysql8.0之前的密码规则是mysql_native ...
- Swing的概述
Swing在Java中是一个包,下面就是它的基本概述:简介: Java为了方便图形界面的实现, 专门设计了类库来满足各种各样的图形元素和用户交互事件, 该类库即为抽象窗口工具箱(Abstract Wi ...
- golang--性能测试和分析
前言 测试分为:压力测试.负载测试.性能测试,功能测试等等,其中在开发过程中开发人员经常要写一些test case unit 自己的模块进行功能测试测和性能.在分析出模块的性能瓶颈后开发人员就需要针对 ...
- day22 模块-collections,time,random,pickle,shelve等
一.引入模块的方式: 1. 认识模块 模块可以认为是一个py文件. 模块实际上是我们的py文件运行后的名称空间 导入模块: 1. 判断sys.modules中是否已经导入过该模块 2. 开辟一个内存 ...
- git ssh密钥配置添加
1. 初次安装git配置用户名和邮箱 $ git config --global user.name "xxx" $ git config --global user.email ...
- ansible使用1
常用软件安装及使用目录 ansible软件2 ### ansible软件部署安装需求#### 01. 需要有epel源 系统yum源(base epel--pip gem) sshpass---e ...
- 软工大作业DB天气项目风险评估
风险 发生概率 损失 风险度 解决方案 项目延期 80% 浪费时间,项目完成进度降低,考试得分低 79% 提前做好详细的准备工作,各方面做好沟通. 工作效率低下 30% 影响进度,使项目延期. 85% ...
- iOS开发学习-nonatomic和atomic的区别
nonatomic是非原子性的,也就是给线程不加原子锁,这样的代码运行效率会更高一点,例如: @property (nonatomic,copy)NSString *userName; @proper ...
- 课堂练习 psp表
项目计划总结表: 日期 编程 完善程序 测试程序 参考资料 日总结 3.20 18:00---19:30 1.5 3.21 9:30----10:00 10:00---10:30 ...