【LeetCode练习题】Next Permutation
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,2
3,2,1
→1,2,3
1,1,5
→1,5,1
题目意思:
给定一组数字,求他的下一个排列。如果已经是最大的排列了,那么就把他调整成最小的排列。而且必须是在num数组中原地调整,不能增加额外的内存分配。
解题思路:
又是一道全排列的题目,我突然就想到了之前的那个求Permutation的题,于是乎第一反应就是不管三七二十一先把你所有的排列都求出来然后存起来,跟给定的排列比较,然后按顺序的下一个就是了。转念一想这样太麻烦了,况且题目还要求了不能分配额外的内存了,所以不可行。
在网上研究了别人解题的算法,大概是这样子的。以num(6 5 4 8 7 5 1)举例:
第一步:先从后往前找,8,7,5,1都是降序排列,我们先找到出现升序的那个地方。即num[2]和num[3],此时我们假设index的值为3。
第二步:在num[index]~num[len-1]中找到比num[2]大的最小数。即num[5]。下标记为exchangeIndex。
第三步:交换num[index-1]和num[exchangeIndex]的值。
第四步:重新调整num[index]~num[len-1]为升序序列。
代码如下:
class Solution {
public:
void nextPermutation(vector<int> &num) {
int index = num.size() - ;
//从右边开始找到第一个不是降序的下标。index值为一对升序数中较大的下标
while(index > ){
if(num[index] > num[index - ]){
break;
}
index--;
}
//如果没有下一个排列,index为0
if(index == ){
sort(num.begin(),num.end());
return ;
}
//在index往后的数字里(降序)找到比[index-1]大的最小的数,下标为exchangeIndex
int exchangeIndex;
for(int i = num.size()-; i >= index; i--){
if(num[i] > num[index-]){
exchangeIndex = i;
break;
}
}
//交换这两个数
swap(num[index-],num[exchangeIndex]);
//index及以后的数字重新按升序排列。
sort(num.begin()+index,num.end()); }
};
【LeetCode练习题】Next Permutation的更多相关文章
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode 266. Palindrome Permutation (回文排列)$
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
随机推荐
- C# 多线程的自动管理(线程池) 基于Task的方式
C# 多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 1. 应用程序中线程把大部分的时间花费在等待状态,等待某个事件发生,然后给予响应.这一般使用 ThreadPool(线程 ...
- Unity扩展 四种Menu的区别
[MenuItem("Tools\AddColor")] : 在Unity菜单中添加一种快捷,执行public static方式 [AddComponentMenu(" ...
- Comet学习资料
什么是Comet: http://baike.baidu.com/view/577938.htm?fr=ala0_1 Comet介绍: http://www.ibm.com/developerwork ...
- js埋点(转载)
页面埋点的作用,其实就是用于流量分析.而流量的意思,包含了很多:页面浏览数(PV).独立访问者数量(UV).IP.页面停留时间.页面操作时间.页面访问次数.按钮点击次数.文件下载次数等.而流量分析又有 ...
- android假设重写onDraw实现一个相似TextView能够显示表情和链接的控件(一)
先看效果图: 写一个超连接支持的对象: /**作为超连接显示的对象*/ public class LinkInfo implements Comparable<LinkInfo>{ pri ...
- 《你必须知道的495个C语言问题》知识笔记及补充
1. extern在函数声明中是什么意思? 它能够用作一种格式上的提示表明函数的定义可能在还有一个源文件里.但在 extern int f(); 和 int f(); 之间并没有实质的差别. 补充:e ...
- android 更改USB显示名称
能力 kernel\drivers\usb\gadget\Android.c 在这个例子中,下列的变化 #define PRODUCT_STRING "Sergeycao" 版权声 ...
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
</pre></center><center style="font-family: Simsun;font-size:14px;"><s ...
- HA高可用的搭建
HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务. 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务.常用 ...
- asp.net + Jquery 实现类似Gridview功能 (一)
不知不觉2015年就过去一半了,由于过年前后公司人员陆续离职(这个...),项目忙不过来,从过年来上班就一直在忙,最近项目终于告一段落,开始步入正轨(不用天天赶项目了).所以最近才有时间写这个东西,可 ...