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

思路:对大小的影响尽可能小=>影响尽可能右侧的位=>右向左扫描,找到第一个<右侧的数(i)

将右边最小的>nums[i]的数(j)与它交换

从小到大排列i之后的数

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size = nums.size();
int tmp, i ,j; for(i = size-; i >= ; i--){
for(j = i+; j < size; j++){
if(nums[i] >= nums[j]) continue; //meet the first num < at least one number at right
for(int k = j+; k < size; k++){ //find the smallest one > nums[i]
if(nums[i] >= nums[k] || nums[j] <= nums[k]) continue; //find the smaller one > nums[i]
j = k;
}
tmp = nums[i];
nums[i]=nums[j];
nums[j]=tmp;
sort(nums.begin()+i+, nums.end());
break;
}
if(j<size) break;
}
if(i<){
sort(nums.begin(), nums.end());
}
}
};

To make codes more simple, we can integrate k iterates into j iterates

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size = nums.size();
int swapIndex = size, tmp, i ,j; for(i = size-; i >= ; i--){
for(j = i+; j < size; j++){
if(nums[j] <= nums[i] || (swapIndex<size && nums[j]>=nums[swapIndex])) continue;
swapIndex = j;
}
if(swapIndex>=size) continue;
tmp = nums[i];
nums[i]=nums[swapIndex];
nums[swapIndex]=tmp;
sort(nums.begin()+i+, nums.end());
break;
}
if(i<){
sort(nums.begin(), nums.end());
}
}
};

31. Next Permutation (Array; Math)的更多相关文章

  1. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  2. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  3. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

  4. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  5. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  6. 刷题31. Next Permutation

    一.题目说明 题目是31. Next Permutation,英文太差看不懂,翻译了一下.才知道是求字典顺序下的下一个排列,不允许使用额外空间.题目难度是Medium! 二.我的实现 首先要进一步理解 ...

  7. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  8. javascript - 内置对象 String/Date/Array/Math

    1.构建对象的方法 <script> //构建对象方法 //第1种 var people = new Object(); people.name = "iwen"; p ...

  9. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. Linux:提示符PS1个性设置

    提示符PS1个性设置 1)默认PS1 echo $PS1 2)个性PS1 #去掉了默认显示的[]号#\e[1;34m\]\u:user名高亮显示并显示颜色#\e[5;33m\]\h:hostname主 ...

  2. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  3. Redis学习笔记-安装篇(Centos7)

    1.安装 这里使用源代码安装的方式,如果你希望使用yum或者rpm包安装的方式,可以百度一下,安装方法可谓多如牛毛. # 下载安装包 # wget http://download.redis.io/r ...

  4. zipkin:调用链显示分析

    为什么使用了httpclient,客户端没有向zipkin server发送日志? 因为我实在main方法中调用的,完事后这个线程就没了:httpclient用的还是异步的发送日志方式:所以没发日志. ...

  5. MAC OX 配置 Tomcat 说明

    1: 首先在官网下载 Tomcat(我选择的是最新的9.0) , http://tomcat.apache.org/ 2:下载完成之后将压缩包解压在/Library/下 可使用快捷键 control+ ...

  6. 使用C++生成1-33中的6个随机数,无重复

    生成1-33中的6个随机数,无重复 ------------------------------------------------------------------------   方法1.每生成 ...

  7. JZ2440 裸机驱动 第5章 GPIO接口

    本章目标:     掌握嵌入式开发的步骤:编程.编译.烧写程序.运行     通过GPIO的操作了解软件如何控制硬件 5.1 GPIO硬件介绍     S3C2440A有130个多功能输入/输出口引脚 ...

  8. Asp.net MVC 出现“Failed to map the path '/'.”错误

    Server Error in '/' Application. Parser Error Description: An error occurred during the parsing of a ...

  9. tomcat启动时SessionIdGeneratorBase.createSecureRandom耗时5分钟的问题

    通常情况下,tomcat启动只要2~3秒钟,突然有一天,tomcat启动非常慢,要花5~6分钟,查了很久,终于在这篇文章找到了解决方案,博主牛人啊. 原文参见:http://blog.csdn.net ...

  10. 条件随机场(CRF)-IIS学习算法

    改进的迭代尺度法(Improved Iterative Scaling),在很多模型求解中用到,比如最大熵.CRFs等,对模型是对数线性模型的似然都适用.这个算法的思想也很简单,通俗的理解就是通过两个 ...