31. Next Permutation 返回下一个pumutation序列
[抄题]:
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,23,2,1 → 1,2,31,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序列的更多相关文章
- 27.Next Permutation(下一个字典序列)
Level: Medium 题目描述: Implement next permutation, which rearranges numbers into the lexicographicall ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31 Next Permutation(下一个全排列)
题目链接: https://leetcode.com/problems/next-permutation/?tab=Description Problem :寻找给定int数组的下一个全排列(要求 ...
- JS window对象 返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL。
返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL. 如果倒退之后,再想回到倒退之前浏览的页面,则可以使用forward()方法,代码如下: window.hi ...
- 力扣——Next Permutation(下一个排列) python实现
题目描述: 中文: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...
- SGU 179 Brackets light(生成字典序的下一个序列)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
随机推荐
- ionic 前端接收到后台推送来的消息后,显示在手机的通知栏上
这里主要用到cordova提供的插件:(在app没有关闭的情况下只要有推送的消息就会有提醒,但是在app关闭的情况下就不会提示) 首先安装cordova-plugin-local-notificati ...
- python 常用的模块
面试的过程中经常被问到使用过那些python模块,然后我大脑就出现了一片空白各种模块一顿说,其实一点顺序也没有然后给面试官造成的印象就是自己是否真实的用到这些模块,所以总结下自己实际工作中常用的模块: ...
- Windows 下最佳的 C++ 开发的 IDE 是什么?
作者:渡世白玉链接:https://www.zhihu.com/question/19589089/answer/30312199来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- python之路——16
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 1.内置函数 1. python 数据类型:int bool 数据结构:dic list tupl ...
- IDEA忽略某些文件
Settings→Editor→File Types 在下方的忽略文件和目录(Ignore files and folders)中添加自己需要过滤的内容 下图为我自己添加过滤的内容,例如:*.im ...
- (整理)EF分页的实现
最近做一个小功能,需要数据分页,因为小框架使用的是EF,因此查询了一下EF的分页. EF分页主要用到了skip和take两个方法: GetListBy(lamda xxxxx).skip(PageSi ...
- The Weather
后台: <?php //接受查询的城市 $city = $_GET['city']; //连接redis $redis = new redis(); $redis->connect(&qu ...
- CountDownLatch两种用法
1.当前线程等待其他线程执行完毕后在执行. 2.模拟高并发场景. 在多线程编程里,CountDownLatch是一个很好的计数器工具. 常用的两个方法: 1.计数器减一 public void cou ...
- Java 四种引用介绍及使用场景
强引用-FinalReference 介绍: 强引用是平常中使用最多的引用,强引用在程序内存不足(OOM)的时候也不会被回收,使用方式: String str = new String("s ...
- Linux 实时查看tomcat 日志--less命令
查看tomcat日志通常用 tail -n 或者 tail -f 或者grep 或者 vi cat等命令去查看异常信息,但是日志是在不停地刷屏,tail是动态的在变的,我们往往期望从日志最后一行往前 ...