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,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序列的更多相关文章
- 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 ...
随机推荐
- windows openssh server 安装试用
使用Windows的可能会知道win10 的已经包好了openssh 服务,但是对于其他机器win 7 windows 2008 ,就需要其他的方法了 还好powershell 团队开发了支持wind ...
- 算法图解 (Aditya Bhargava 著)
第1章 算法简介第2章 选择排序第3章 递归第4章 快速排序第5章 散列表第6章 广度优先搜索第7章 狄克斯特拉算法第8章 贪婪算法第9章 动态规划第10章 K最近邻算法第11章 接下来如何做 第1章 ...
- C# 调用打印机 打印 Excel
打印 Excel 模板 大体思路,通过NPOI操作Excel文件,通过Spire将Excel转成图片,将图片传给系统打印. Spire是收费工具,在微软库中下载Free版本. #region 打印所用 ...
- 关于各种BUF源语的研究
关于各种BUF源语的研究 资料来源: 单端信号需要用到的BUF 关于这些源语的约束: 增大驱动电流 关于管脚的上拉与下拉约束: ODDR的两种操作模式 关于ODDR输出时钟的应用 为什么ODDR需要这 ...
- 初识DMA
初识DMA 关于AXI4-Memory Map 与 AXI4-Stream之间的转换: 查阅UG1037 重点关注DataMover这一块 此图似乎有错误之处,需要再次确认.
- NPOI 操作Word
/// <summary> /// 替换word中指定内容 /// </summary> /// <param name="wordPath"> ...
- Web GIS系统相关
最近研究了百度 echarts 的一个很炫酷的示例: http://gallery.echartsjs.com/editor.html?c=xrJHCfsfE- 看了代码发现了很多不懂1东西,研究研究 ...
- AttributeError: 'module' object has no attribute 'main'
本机环境:ubuntu16.04, ros-kinetic $ roscore 报错 Traceback (most recent call last): File , in <module& ...
- Backbone 学习总结
1.Backbone描述 官网描述:(1)Provides client-side app structure (2)Models to repents data (3)View to hook up ...
- 团队第一次 # scrum meeting
github 本此会议项目由PM召开,召开时间为4-1日晚上9点 召开时长30分钟 任务表格 袁勤 重新搭建原本的项目 https://github.com/buaa-2016/BuaaPhyLabB ...