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 ...
随机推荐
- git基本技巧及进阶
基本技巧 1. 安装后的第一步 在安装好git后,你第一件该做的事是设置你的名字和电子邮箱,因为每次提交都要用到这些信息: $ git config --global user.name " ...
- 关于由CSS2.1所提出的的BFC的理解与样例
今天在这里谈谈css中BFC.“BFC”是Block Formatting Context的缩写,这个概念是由CSS2.1提出来的,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用.满 ...
- linux密码的更改
找到UTF-8,在后面空格后输入init=/bin/sh 然后CHRL+X启动 进入到这个界面,输入mount -o remount,rw / 再输入touch / .autorelabel ,然后 ...
- 最优雅,高效的javascript字符串拼接
这种方式是es6的语法.使用键盘1左边的那个字符 `` 拼接, 再加上js自带的模板引擎拼接字符串非常快速.这东西也没什么高深的,看几个例子就懂了. console.log(`<xml> ...
- Why sql is called structured query language?1 - 12
SQL has much to do with a researcher at IBM, Edgar F. (Ted) Codd, an Oxford-trained mathematician, w ...
- Java网络编程及安全
一.实验内容: 1.运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成代码,一人加密后通过TCP发送: 注:加密使用AES或者 ...
- (转)jQuery插件 -- Form表单插件jquery.form.js
beforeSubmit: validate function validate(formData, jqForm, options) { //在这里对表单进行验证,如果不符合规则,将返回false来 ...
- WebStrom 10 注册码(转)
webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA ...
- schematool -dbType mysql -initSchema hive startup failed...try this
schematool -dbType mysql -initSchema hive startup failed
- web设计中那些因素可能影响网站后期优化
web设计中那些因素可能影响网站后期优化. 1.网站代码的简洁实用性.网站源文件html代码.js代码.css代码等应尽可能的压缩处理.能用jquery-min.js的最好不要用jquery.js:c ...