Next Permutation

Given a list of integers, which denote a permutation.

Find the next permutation in ascending order.

Notice

The list may contains duplicate integers.

Example

For [1,3,2,3], the next permutation is [1,3,3,2]

For [4,3,2,1], the next permutation is [1,2,3,4]

Analysis:

In order to find the next permutation, we need to begin from the right most and find a number which is less than its right neighbor. And then switch it with the smallest number on its right side, but that smallest number must be greater than the number to be switched.

  1. class Solution {
  2. public void nextPermutation(int[] nums) {
  3. int i = nums.length - ;
  4. // 从最右边开始,首先找到一个值而且该值比它右边那个更小,这样我们可以把该值和它右边最小的值交换。
  5. // example: 1329876, the next one is 1362789
  6. while (i >= && nums[i + ] <= nums[i]) {
  7. i--;
  8. }
  9. if (i >= ) {
  10. int j = nums.length - ;
  11. // we need to find the min value from i + 1 to j which is greater than nums[i]
  12. while (j >= && nums[j] <= nums[i]) {
  13. j--;
  14. }
  15. swap(nums, i, j);
  16. }
  17. reverse(nums, i + );
  18. }
  19.  
  20. private void reverse(int[] nums, int start) {
  21. int i = start, j = nums.length - ;
  22. while (i < j) {
  23. swap(nums, i, j);
  24. i++;
  25. j--;
  26. }
  27. }
  28.  
  29. private void swap(int[] nums, int i, int j) {
  30. int temp = nums[i];
  31. nums[i] = nums[j];
  32. nums[j] = temp;
  33. }
  34. }

Previous Permutation

Given a list of integers, which denote a permutation.

Find the previous permutation in ascending order.

Notice

The list may contains duplicate integers.

Example

For [1,3,2,3], the previous permutation is [1,2,3,3]

For [1,2,3,4], the previous permutation is [4,3,2,1]

Analysis:

From the right most, find a number which is greater than its right neighbor, then switch it with the largest number on its right side, but that largest number must be less than the number to be switched.

  1. public class Solution {
  2. /**
  3. * @param nums: A list of integers
  4. * @return: A list of integers that's previous permuation
  5. */
  6. public ArrayList<Integer> previousPermuation(ArrayList<Integer> numss) {
  7.  
  8. if (numss == null || numss.size() <= )
  9. return numss;
  10.  
  11. Integer[] nums = new Integer[numss.size()];
  12. numss.toArray(nums);
  13.  
  14. int k = nums.length - ;
  15.  
  16. while (k >= && nums[k] <= nums[k + ]) {
  17. k--;
  18. }
  19. // test case 211189
  20. if (k != -) {
  21. int p = nums.length -;
  22. while (p > k) {
  23. if (nums[k] > nums[p]) {
  24. swap(nums, k, p);
  25. break;
  26. }
  27. p--;
  28. }
  29. swapAll(nums, k + , nums.length - );
  30. } else {
  31. swapAll(nums, , nums.length - );
  32. }
  33. return new ArrayList<Integer>(Arrays.asList(nums));
  34. }
  35.  
  36. public void swap(Integer[] nums, int i, int j) {
  37. int temp = nums[i];
  38. nums[i] = nums[j];
  39. nums[j] = temp;
  40. }
  41.  
  42. public void swapAll(Integer[] nums, int i, int j) {
  43. while (i < j) {
  44. swap(nums, i, j);
  45. i++;
  46. j--;
  47. }
  48. }
  49. }
 

Next Permutation & Previous Permutation的更多相关文章

  1. leetcode_1053. Previous Permutation With One Swap

    1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...

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

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

  3. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  4. Previous Permutation

    Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...

  5. 【leetcode】1053. Previous Permutation With One Swap

    题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...

  6. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

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

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

  8. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  9. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

随机推荐

  1. ubuntu 16.04 部署 pypy+nginx+uwsgi+django(详细)

    1.nginx                                                                                             ...

  2. Java中线程同步的理解 - 其实应该叫做Java线程排队

    Java中线程同步的理解 我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread). 线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可 ...

  3. shiro异常类型

    <!-- 身份认证异常 --> <!-- 身份令牌异常,不支持的身份令牌 --> org.apache.shiro.authc.pam.UnsupportedTokenExce ...

  4. javascript循环事件只响应最后一次的问题处理

    在所有的面向对象编程语言中,只要涉及到逻辑的代码,常见的问题都是循环创建很多个对象UI,在循环体中对这些对象添加事件.如果不做处理,和其他地方一样的添加事件,其结果都是只响应最后一次循环之后的结果.原 ...

  5. 【bzoj1087】互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  6. Merge Intervals - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

  7. boost::asio::deadline_timer(理解)

    并发与并行: 并发和并行从宏观上来讲都是同时处理多路请求的概念.但并发和并行又有区别,并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生. 1.Timer.1 - 使 ...

  8. navicat链接mysql

    navicat链接mysql

  9. C# 在程序中控制IIS服务或应用程序池关闭重启

    //停止IIS服务 ServiceController sc = new ServiceController("iisadmin"); if(sc.Status=ServiceCo ...

  10. 翻译: 星球生成 I

    翻译: 星球生成 I 本文翻译自Planet Generation - Part I 译者: FreeBlues 以下为译文: 概述 我一直是一个过程内容生成的爱好者, 它允许你创建一个甚至不断改变的 ...