[抄题]:

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. cannot find package "context"

    导入  github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...

  2. 承接AR定制AR项目外包(正规公司,内附案例)

    京团队长年承接AR项目外包 咨询QQ:372900288  微信:liuxiang0884 以下是AR项目案例演示,索取更多案例请通过以上方式在线联系我们

  3. Java Web参考资料

    Maven Maven学习 Eclipse 使用Maven 构建Web项目的最佳方式:总的来说就是先要修改项目的Project facets来更改Java版本.Servlet版本. 错误Cannot ...

  4. 报错:org.apache.hadoop.hive.metastore.HiveMetaException: Failed to get schema version.

    报错环境: CDH中集成的hive服务,启动报错,所以初始化一下元数据. 配置文件:/etc/hive/conf hive-site.xml 命令目录:/opt/cloudera/parcels/CD ...

  5. U3D学习14-一阶段学习总结

    一.UGUI界面拖拽 1.物品类中继承以下5个接口 命名空间: UnityEngine.EventSystem; IBeingDragHandler (OnBeingDrag) IDragHandle ...

  6. ef join查询

    temp = temp.OrderByDescending(s => s.CreateTime).Skip((param.PageIndex - ) * param.PageSize).Take ...

  7. WordPress版微信小程序2.4版发布

    自从发布2017年9月16日WordPress版微信小程序2.2.8版本后,这个一个多月来,WordPress版微信小程序,在经过一些比较小的更新后,今天发布阶段性的版本:2.4版 .这版本主要是功能 ...

  8. 编写高质量java代码151个建议

    http://blog.csdn.net/aishangyutian12/article/details/52699938 第一章  Java开发中通用的方法和准则 建议1:不要在常量和变量中出现易混 ...

  9. HTML---仿网易新闻登录页

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Vue 封装的noData组件

    <template> <div :style="{color: fontColor}" :class="['noDataView', iconType] ...