【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," ...
随机推荐
- ArcEngine10在VS2010中编译问题
原文 http://www.gisall.com/html/47/122747-4141.html 问题描述: 前段时间装了个VS2010,用ArcEngine10在VS2010中基于.Net Fra ...
- ###Git 基础图解、分支图解、全面教程、常用命令###
一.Git 基础图解 转自:http://www.cnblogs.com/yaozhongxiao/p/3811130.html Git 图解剖析 git中文件内容并没有真正存储在索引(.git/in ...
- STL中,迭代器的分类
五类迭代器如下: 1.输入迭代器:只读,一次传递 为输入迭代器预定义实现只有istream_iterator和istreambuf_iterator,用于从一个输入流istream中读取.一个输 ...
- ubuntu 文件readonly的问题: W10: Warning: Changing a readonly file 解决办法
日前,笔者遇到一个奇怪且让人蛋疼的问题,借用别人的话"大家在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warn ...
- STL中的set集合容器进行集合运算:并、交、差实例
集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...
- MySql5.1在Win7下的安装与重装问题的解决
痛苦啊痛苦,我也不知道这两天怎么了.上班没有精神,还打瞌睡,下班后又感觉很累.精力集中不起来. 这篇花了我好久的时间,我效率这么差,~\(≧▽≦)/~. 软件包下载 首先单击mysql-5.1.53- ...
- 结构体 + typedef
简单结构体 struct student{ char name[20]; //可以用scanf或者直接赋值 *如果用char *name 在用scanf时没有内存接收 long id; int ...
- ps 网页布局
910 1680 找一个页面作为参考 双击小手回到正常视角 新建组 把他们放到一个组里 新建组改名(创意专家) 放入一个图片 内发光投影 Shiftalt 复制 新建组 改名创意案 ...
- Intel 编译Boost库
C:\Windows\SysWOW64\cmd.exe /E:ON /V:ON /K ""C:\Program Files (x86)\Intel\Composer XE 2013 ...
- Simple Daemon Shell
PROPATH="/var/www/html/" PROGRAM="vertical" LOGNAME="/tmp/monitor.vertical. ...