[抄题]:

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 and use only constant 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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

完全不知道怎么操作啊:从后往前找递增,再把递减的尾巴整个reverse一遍,变成递增。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

把递减的尾巴整个reverse一遍,变成递增。i和后面交换,确保更加递增。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

从合理性的角度考虑:reverse(nums, i + 1, len - 1); 则i可以从-1开始。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

把递减的尾巴整个reverse一遍,变成递增。i和后面交换,确保更加递增。

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public void nextPermutation(int[] nums) {
int len = nums.length; //corner case
if (nums == null || len < 2) return ; //find the first ascending i from len - 2
int i = len - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) i--; if (i >= 0) {
//find the max j from the len - 1
int j = len - 1;
while (nums[i] >= nums[j]) j--; //swap i & j
swap(nums, i, j);
}
//reverse from i+1 to len - 1
reverse(nums, i + 1, len - 1);
System.out.println("i = " + i);
} public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
} public void reverse(int[] nums, int i, int j) {
while (i < j)
swap(nums, i++, j--);
}
}

31. Next Permutation 返回下一个pumutation序列的更多相关文章

  1. 27.Next Permutation(下一个字典序列)

    Level:   Medium 题目描述: Implement next permutation, which rearranges numbers into the lexicographicall ...

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

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

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

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

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

    题目链接: https://leetcode.com/problems/next-permutation/?tab=Description   Problem :寻找给定int数组的下一个全排列(要求 ...

  5. JS window对象 返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL。

    返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL. 如果倒退之后,再想回到倒退之前浏览的页面,则可以使用forward()方法,代码如下: window.hi ...

  6. 力扣——Next Permutation(下一个排列) python实现

    题目描述: 中文: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...

  7. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

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

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

  9. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

随机推荐

  1. 开源WHMCS支付宝当面付和即时到账插件

    开源WHMCS支付宝当面付和即时到账插件 链接: https://pan.baidu.com/s/1i5HU4hn 密码: crq7

  2. PythonStudy——字符串常用操作 String common operations

    # 1.字符串的索引取值: 字符串[index]# 正向取值从0编号,反向取值从-1编号 s1 = '123abc呵呵' t_s = ' # 取出c print(s1[5], s1[-3]) # 2. ...

  3. kvm创建新虚拟机

    安装图形化管理界面yum install virt-manager -y 安装好之后 新建虚拟机,我使用的方法是使用ISO镜像文件安装 选择镜像 设置内存 如此,一步一步走下去即可,不再截图 创建好之 ...

  4. winform中devexpress bindcommand无效的解决方法

    正常绑定,编译运行无报错,但无法执行command fluentAPI.BindCommand(commandButton, (x, p) => x.DoSomething(p), x => ...

  5. day50 django第一天 自定义框架

    主要内容: 1.http协议 2.web框架 3.Django 1.http协议 1.1 http协议的简介 超文本传输协议(英文:Hyper Text Transfer Protocol,HTTP) ...

  6. MQTT研究之mosquitto:【环境搭建】

    环境信息: 1. Linux Centos7.2 环境,CPU 2核,内存8G. 2. mosquitto版本:mosquitto-1.5.4 官网:http://mosquitto.org/down ...

  7. 关于set_input_delay的用法分析

    关于set_input_delay的用法分析 数据分为了系统同步和源同步: 对于下降沿采集数据的情况,当下降沿时钟延迟dv_afe到达无效数据最左端时,图中1位置,为最小延时,即采集不到有效数据的临界 ...

  8. Delphi操作剪贴板

    Windows使用剪贴板观察器和观察链.剪贴板观察器是一个显示剪贴板当前内容的窗口.            通常它应该至少能显示三种普通格式的内容:文字CF_TEXT.位图CF_BITMAP.元文件C ...

  9. lambda函数的特性

    lambda表达式可以理解为一种抽象的函数实现方法,这种方式只有最基本的三个步骤:给与参数,表达式实现,返回结果.这种方式非常干净,减少了内存的使用,整个程序少了函数的污染,代码格式也会更为简练.但在 ...

  10. Python 语言之 map/reduce

    1.相关文献 大名鼎鼎的Google论文<MapReduce: Simplified Data Processing on Large Clusters> 对应的中文翻译<MapRe ...