1. import java.util.Arrays;
  2. /**
  3. * Source : https://oj.leetcode.com/problems/next-permutation/
  4. *
  5. * Created by lverpeng on 2017/7/13.
  6. *
  7. * * Implement next permutation, which rearranges numbers into the lexicographically next
  8. * greater permutation of numbers.
  9. *
  10. * If such arrangement is not possible, it must rearrange it as the lowest possible order
  11. * (ie, sorted in ascending order).
  12. *
  13. * The replacement must be in-place, do not allocate extra memory.
  14. *
  15. * Here are some examples. Inputs are in the left-hand column and its corresponding outputs
  16. * are in the right-hand column.
  17. *
  18. * 1,2,3 → 1,3,2
  19. * 3,2,1 → 1,2,3
  20. * 1,1,5 → 1,5,1
  21. *
  22. */
  23. public class NextPermutation {
  24. /**
  25. * 寻找多个数字组成的数字序列中的下一个,比如:1,2,3组成的序列
  26. * 123,132,213,231,312,312
  27. *
  28. * 规律如下:
  29. * 从右向左找到第一个num[i] > num[i-1]
  30. * 然后从右向左找到第一个num[k] > num[i-1]
  31. * 交换两个位置
  32. * 对num[i-1]后面元素排序
  33. *
  34. * 边界条件:i = 1的时候还没找到num[i-1]就把整个数字的各个位翻转顺序
  35. *
  36. *
  37. * @param num
  38. * @return
  39. */
  40. public void nextPermutation (int[] num) {
  41. for (int i = num.length - 1; i > 0; i--) {
  42. if (num[i] > num[i-1]) {
  43. int k = num.length - 1;
  44. while (num[i-1] > num[k]) {
  45. k --;
  46. }
  47. // swap
  48. int temp = num[i-1];
  49. num[i-1] = num[k];
  50. num[k] = temp;
  51. reverse(num, i, num.length - 1);
  52. return ;
  53. }
  54. // 边界情况,已经是最大了,转化为最小
  55. if (i == 1) {
  56. reverse(num, 0, num.length - 1);
  57. return ;
  58. }
  59. }
  60. }
  61. private void reverse (int[] arr, int start, int end) {
  62. if (start < 0 || end > arr.length - 1 || start > end) {
  63. return;
  64. }
  65. while (start < end) {
  66. int temp = arr[start];
  67. arr[start] = arr[end];
  68. arr[end] = temp;
  69. end --;
  70. start ++;
  71. }
  72. }
  73. public static void main(String[] args) {
  74. NextPermutation nextPermutation = new NextPermutation();
  75. int[] arr1 = new int[]{1, 2, 3, 4};
  76. nextPermutation.nextPermutation(arr1);
  77. System.out.println(Arrays.toString(arr1));
  78. int[] arr2 = new int[]{1, 3, 2, 4};
  79. nextPermutation.nextPermutation(arr2);
  80. System.out.println(Arrays.toString(arr2));
  81. int[] arr3 = new int[]{4,3,2,1};
  82. nextPermutation.nextPermutation(arr3);
  83. System.out.println(Arrays.toString(arr3));
  84. }
  85. }

leetcode — next-permutation的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Next Permutation 下一个排列

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

  5. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  9. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

  10. LeetCode Find Permutation

    原题链接在这里:https://leetcode.com/problems/find-permutation/description/ 题目: By now, you are given a secr ...

随机推荐

  1. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

  2. MacBook上那些好用的工具们

    https://blog.csdn.net/qq_33833327/article/details/78454703

  3. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

  4. 转发:RSA实现JS前端加密,PHP后端解密

    web前端,用户注册与登录,不能直接以明文形式提交用户密码,容易被截获,这时就引入RSA. 前端加密 需引入4个JS扩展文件,jsbn.js.prng4.js.rng.js和rsa.js. <h ...

  5. bootstrap table使用参考

    https://www.cnblogs.com/landeanfen/p/5821192.html  转载 阅读目录 一.x-editable组件介绍 二.bootstrapTable行内编辑初始方案 ...

  6. Day11 (黑客成长日记) 爬取网站图片

    #导入第三方库# coding:utf-8import requests,re #找到需要爬取的网站'http://www.qqjia.com/sucai/sucai1210.htm' #1>获 ...

  7. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  8. 基于jmeter的性能测试平台(一)分布式jmeter搭建

    (1)概述 一台windows虚拟机作为controller,3台Linux虚拟机作为agent. 第一步是在所有虚拟机上安装JDK,版本最好是一样的,然后就是下载安装jmeter,网上资料很多这里不 ...

  9. 基于fpga的vga学习(3)

    本次学习如何通过vga发送数字.文字.字母, 首先利用建模软件,将想要发送的数据通过数学建模转换,这里我用的软件是PCtoLCD,具体效果如下 这里可以看出,建模将数据装换成0和1,一个字母用16x8 ...

  10. 自定义导航栏 tabBarController 笔记

    #import "LeeNavigationController.h" @interface LeeNavigationController () @end @implementa ...