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. 20181009-7 选题 Scrum立会报告+燃尽图 06

    Scrum立会报告+燃尽图(06)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2196 一.小组介绍 组长:刘莹莹 ...

  2. PostgreSQL锁级别及什么操作获取什么锁

    表级锁 大多数的表级锁是由内置的 SQL 命令获得的,但他们也可以通过锁命令来明确获取.可使用的表级锁包括: 访问共享(ACCESS SHARE) - SELECT 命令可在查询中引用的表上获得该锁. ...

  3. myeclipse单步调试

    如何进行myclipse的单步调式与跟踪?希望大虾们详细点,多谢. 打断点,然后运行,进debug试图,按F6执行一行,按F5是钻进去执行 追问 朋友,能详细点吗? 本人是初学 回答 如图 如若成功请 ...

  4. 《DSP using MATLAB》示例Example 10.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  5. Html页面Dom对象之Event

    HTML DOM Event 对象 实例 哪个鼠标按钮被点击? 光标的坐标是? 被按的按键的 unicode 是? 相对于屏幕,光标的坐标是? shift 键被按了吗? 哪个元素被点击了? 哪个事件类 ...

  6. streamsets microservice pipeline 试用

    实际上还是一个pipeline,只是添加了一些规则以及内嵌的http server 方便我们对于基于http 或者类似轻量 协议数据的处理 基本环境 使用docker&& docker ...

  7. Telnet远程重启路由器TP-LINK

    突然断网,以前房东的路由器管理页面可以打开,今天突然间就打不开了.ping了下,可以ping通,于是就想起了房东的路由器是TP-LINK的 可以 telnet登陆的.每次,断网,我都会重启房东的路由器 ...

  8. 从 FastAdmin 项目上学了什么?

    从 FastAdmin 项目上学了什么? 接触到 FastAdmin 我学了好多,自己记录一下 Xmind git 系统学习了 Javascript jQuery 重新开始玩 ThinkPHP 开始记 ...

  9. 关于filter web api mvc 权限验证 这里说的够详细了。。。

    参考:http://www.cnblogs.com/willick/p/3331520.html Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是对MVC框架处理客户端请求注入额外 ...

  10. [转][ASP.NET]ASP.NET 预编译网站

    [转自]https://msdn.microsoft.com/zh-cn/library/ms227430(v=vs.80).aspx C:\Windows\Microsoft.NET\Framewo ...