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 ...
随机推荐
- Python学习基本
刚开始学习是看了这个网站: 廖雪峰的官方网站 http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92707 ...
- 不使用ASP.NET中的服务器控件将如何上传文件?
遇到文件的上传时,可能会有大部分的开发者喜欢使用服务器控件,虽然很方便,但是却不能很好的控制,不具灵活性. 现给出例子,使用html标签语言灵活的控制文件的上传. 1.html部分 <input ...
- 【转】Matlab练习程序(各向异性扩散)
http://www.cnblogs.com/tiandsp/archive/2013/04/18/3029468.html 主要是用来平滑图像的,克服了高斯模糊的缺陷,各向异性扩散在平滑图像时是保留 ...
- hibernate整合spring开发的时候遇到的一些小问题
1 在spring整合hibernate开发的时候,在数据源里面配置show_sql为true,但是在实际查询的时候并没有打印sql语句,正确的解决方案为: 把<prop key="s ...
- js键盘事件
弱鸡今天在写键盘事件,发生一个小bug,排查了1小时(">皿<)可恶的浏览器竟然还不报错!!原因是将e.ctrlKey写成了e.ctrLKey,o(゚Д゚)っ 想想都要生气< ...
- 学习git config配置文件
设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...
- 重置了下系统好多关于mysql密码的文章都很渣拷分好的备用
方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass ...
- Python的平凡之路(17)
一.认识jQuery jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设 ...
- 关于iOS和OS X废弃的API你需要知道的一切
如你所知,已废弃(Deprecated)的API指的是那些已经过时的并且在将来某个时间最终会被移除掉的方法或类.通常,苹果在引入一个更优秀的API后就会把原来的API给废弃掉.因为,新引入的API通常 ...
- 数据库Mysql学习笔记(一)
Mysql 数据库是数据库初学者最佳的选择的,其语法简单,采用的非底层的SQL语言定义(DDL).操作(DML).控制(DCL)数据库. 入门知识:服务器.库.表. (1)安装Mysql服务器.配置文 ...