刷题31. Next Permutation
一、题目说明
题目是31. Next Permutation,英文太差看不懂,翻译了一下。才知道是求字典顺序下的下一个排列,不允许使用额外空间。题目难度是Medium!
二、我的实现
首先要进一步理解题目,以1->2->3为例,字典顺序如下:
(1) 1->2->3;
(2) 1->3->2;
(3) 2->1->3;
(4) 2->3->1;
(5) 3->1->2;
(6) 3->2->1;
(7) 1->2->3;
如何从(1)-> (2) ->(3)-> (4) ->(5)-> (6) ->(7)实现状态转换?以(3)->(4)为例:
从列表lists的最右边起,
if(lists[t] < lists[t-1]) {
swap(lists[t-1],max{lists[t]...lists[listSize-1]})
sort(lists[t],lists[listSize-1]);
}
从(6)->(7),sort(lists[0],lists[listSize-1])即可。
代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
void nextPermutation(vector<int>& nums){
if(nums.size()<=1) return ;
bool flag = false;
for(int t=nums.size()-1;t>0;t--){
if(nums[t]>nums[t-1]){
//find the smallest between nums[t] to nums[t-1]
flag = true;
int max = nums[t];
int maxIndex = t;
for(int k=nums.size()-1;k>=t;k--){
if(nums[t-1]<nums[k]){
max = nums[k];
maxIndex = k;
break;
}
}
int tmp = nums[t-1];
nums[t-1] = nums[maxIndex];
nums[maxIndex] = tmp;
//从t..size()-1重新排序
int len = nums.size()-t;
for(int s=0;s<(len+1)/2;s++){
tmp = nums[t+s];
nums[t+s] = nums[nums.size()-s-1];
nums[nums.size()-s-1] = tmp;
}
break;
}
}
if(!flag){
int tmp,len = nums.size();
for(int t=0;t<(len+1)/2;t++){
tmp = nums[t];
nums[t] = nums[len-t-1];
nums[len-t-1] = tmp;
}
}
}
};
int main(){
Solution s;
vector<int> v;
v = {1,3,2};
s.nextPermutation(v);
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
v = {5,4,7,5,3,2};
s.nextPermutation(v);
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
v = {3,2,1};
s.nextPermutation(v);
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
v = {1,5,1};
s.nextPermutation(v);
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
三、改进措施
提交后,性能如下:
Runtime: 8 ms, faster than 78.45% of C++ online submissions for Next Permutation.
Memory Usage: 8.6 MB, less than 88.17% of C++ online submissions for Next Permutation.
差不多了,就不优化了。
刷题31. Next Permutation的更多相关文章
- leecode刷题(31) -- 回文数
leecode刷题(31) -- 回文数 回文数 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输 ...
- 【leetcode刷题笔记】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 面试刷题31:分布式ID设计方案
面试中关于分布式的问题很多.(分布式事务,基本理论CAP,BASE,分布式锁)先来一个简单的. 简单说一下分布式ID的设计方案? 首先要明确在分布式环境下,分布式id的基本要求. 1, 全局唯一,在分 ...
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- 牛客网刷题(纯java题型 31~60题)
牛客网刷题(纯java题型 31~60题) 重写Override应该满足"三同一大一小"三同:方法名相同,参数列表相同,返回值相同或者子类的返回值是父类的子类(这一点是经过验证的) ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- 剑指offer刷题
1.面试题43. 1-n整数中1出现的次数 输入一个整数 n ,求1-n这n个整数的十进制表示中1出现的次数. 例如,输入12,1-12这些整数中包含1 的数字有1.10.11和12,1一共出现了5次 ...
- leetcode刷题总结一
大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...
- 31. Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
随机推荐
- 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/
developerWorks 中国 技术主题 Open source 文档库 使用 CAS 在 Tomcat 中实现单点登录 单点登录(Single Sign On , 简称 SSO )是目前比较流行 ...
- php:数据库封装类
<?phpclass DBDA{ public $host="localhost"; public $uid="root"; publi ...
- Object.getOwnPropertyDescriptors()
Object.getOwnPropertyDescriptors() 前面说过,Object.getOwnPropertyDescriptor方法会返回某个对象属性的描述对象(descriptor). ...
- ES 模糊查询
参考:https://blog.csdn.net/u011262847/article/details/78007119
- Mapreduce实例——WordCount
实验步骤 切换目录到/apps/hadoop/sbin下,启动hadoop. cd /apps/hadoop/sbin ./start-all.sh 2.在linux上,创建一个目录/data/map ...
- Python测试进阶——(1)安装Python测试相关模块
安装python 安装pip yum -y install epel-release yum -y install python-pip 安装psutil 参考:https://www.cnblogs ...
- elasticsearch kibana logstash(ELK)的安装集成应用
官网关于kibana的学习指导网址是:https://www.elastic.co/guide/en/kibana/current/index.html Kibana是一个开源的分析和可视化平台,设计 ...
- centos下离线安装zip和unzip
首先如果你的centos可以联网,那可以不用看了,直接yum install -y zip unzip就行,非常的痛快! 如果不能联网,像我一样,只能用vpn连上去,做了点限制.那就非常烦了,yum了 ...
- phpMydmin的GetShell思路
phpMyadmin简介 phpMyadmin是一个以PHP为基础的MySQL数据库管理工具,使网站管理员可通过Web接口管理数据库 . 信息收集 此部分主要需要收集的是网站物理路径,否则后续无法通过 ...
- Java之反射 — 用法及原理
Java之反射 - 用法及原理 定义 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象 ...