【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,23,2,1→1,2,31,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," ...
随机推荐
- Qt编程之QImage类小结
最近用Qt做图像处理,以下references是需要用到的 references: http://blog.csdn.net/lyc_daniel/article/details/9193881 ht ...
- Best Time to Buy and Sell Stock 解答
Question Say you have an array for which the ith element is the price of a given stock on day i. If ...
- Remove Duplicates from Sorted List II 解答
Question Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist ...
- grep, egrep, fgrep笔记
grep, egrep, fgrep grep: 根据模式搜索文本,并将符合模式的文本行显示出来.Pattern: 文本字符和正则表达式的元字符组合而成匹配条件 grep [options] PATT ...
- sybase用户管理(创建、授权、删除)
一.登录用户管理:1.创建用户:sp_addlogin loginame, passwd [, defdb] [, deflanguage] [, fullname] [, passwdexp] [, ...
- Spring Ioc知识整理
Ioc知识整理(一): IoC (Inversion of Control) 控制反转. 1.bean的别名 我们每个bean元素都有一个id属性,用于唯一标识实例化的一个类,其实name属性也可用来 ...
- 清理SQL数据库日志
Use DBSelect NAME,size From sys.database_files ALTER DATABASE DB SET RECOVERY SIMPLE WITH NO_WAIT AL ...
- 重写TextView,实现圆形背景,文本居中显示
最近,在做考试试题排版,产品提出题号希望显示成圆形背景,序号文本居中显示. (有点问题:文本没有绝对居中,暂时没做处理.) 为此,我采取的方式是重写TextView的onDraw方法,绘制一个圆形背景 ...
- SqlLikeAttribute 特性增加 左、右Like实现
SqlLikeAttribute 特性原来只实现了全Like,今天增加左.右Like实现 更新时间:2016-04-30 /// <summary> /// 获取查询条件语句 /// &l ...
- 写sql语句注意事项
做管理系统的,无论是bs结构的还是cs结构的,都不可避免的涉及到数据库表结构的设计,sql语句的编写等.因此在开发系统的时候,表结构设计是否合理,sql语句是否标准,写出的sql性能是否优化往往会成为 ...