LeetCode 【31. 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
这个最开始我比较不能理解题目的意思,后来才明白表示两个排序之间没有其他的排序的意思是,比如1,2,3,4下一个排序是1,2,4,3 --> 1,3,2,4 --> 1,3,4,2 --> 1,4,2,3 -->.....
就是说第二个排序组成的连续数字,比前面的大,并且中间没有其他的组合,如果已经是4,3,2,1了,那么下一个就是1,2,3,4即回到开头。
我们来研究一下上面[1,2,3,4]的排序过程,比如比1,2,3,4大的是1,2,4,3怎么出来的呢?再看看1,3,4,2 ---> 1,4,2,3
1.找到nums[i] > nums[i-1]
2.找出i-nums.size()-1之间比nums[i-1]大的最小值,交换这个值与nums[i-1]
3.对i-1到nums.size()-1之间的元素进行排序
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int end = nums.size()-1;
while( end > 0 ){
if( nums[end] > nums[end-1] ){
break;
}
else{
end--;
}
}
if( end == 0 ){
sort(nums.begin(),nums.end());
}
else{
int min = nums[end];
int index = end;
for( int i = nums.size()-1; i > end; i-- ){
if( nums[i] < min && nums[i] > nums[end-1] ){
min = nums[i];
index = i;
}
}
swap(nums[index],nums[end-1]);
sort(nums.begin()+end,nums.end());
}
}
};
LeetCode 【31. Next Permutation】的更多相关文章
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- LeetCode OJ 31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 【190. Reverse Bits】
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- leetcode problem 31 -- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode【第1题】Two Sum
准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode【217. Contains Duplicate】
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode【第217题】Contains Duplicate
题目: ''' Given an array of integers, find if the array contains any duplicates. Your function should ...
随机推荐
- Visual Studio 不生成.vshost.exe和.pdb文件的方法【转】
Visual Studio 不生成.vshost.exe和.pdb文件的方法[转] 使用Visual Studio编译工程时,默认设置下,即使选择了「Release」时也会生成扩展名为「.vshost ...
- build and set proxy in Ubuntu
build http://www.2cto.com/os/201310/249690.html set http://www.360doc.com/content/11/1112/00/2617151 ...
- Kindle使用的一些方法
最大的好处就是方便,买书便宜,到手我就买了六部书,十块钱不到,以纸书的价格一本都买不到,能够买一些一直想读一下,但又担心读不下去的书.而且买了之后完全不用担心书柜收纳不下了.另外很轻便,放在包里上下班 ...
- C语言程序设计第八次作业
一.学习内容 本次课学习了一维数组的基本知识,需要大家对如下知识点进行总结: 1. 数组的定义,数组元素的引用,数组的初始化和赋值. 2. 明确以下问题:能否给数组名赋值?为什 ...
- jquery判断div是否隐藏实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL注入的常用函数和语句
1.系统函数 version() Mysql版本user() 数据库用户名database() 数据库名@@datadir 数据库路径@@version_compile_os 操 ...
- oracle xmltype导入并解析Excel数据 (四)特别说明
1.Excel导出,此处没有给出 2.错误原因在中间表,T_EXCEL_IMPORT_GENERATION,其中errormsg不为空的数据 3,中间表入库过程: 需要自己实现,为一个存储过程,存储过 ...
- Maven基础知识(转)
文章摘自http://www.cnblogs.com/xing901022/p/4170248.html 谢谢楼主的总结,界面设计的很好看! 一.什么是Maven Maven是一个用于项目构建的工具, ...
- RaspBMC使用攻略与问题总结
XBMC最初叫Xbox Media Center,是xbox的游戏控制器,后来移植到其他操作系统 XBMC在v14后改名为Kodi RaspBMC是XBMC在Rasperry PI上定制的linux发 ...
- MVC 架构
MVC 模式是一种严格实现应用程序个部分隔离的框架模式.这种"隔离"也叫"分离关注点" 通俗名称:"松耦合" 松耦合的应用程序价格设计方式, ...