Next Permutation & Previous Permutation

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 public class Solution {
 2     /**
 3      * @param nums: an array of integers
 4      * @return: return nothing (void), do not return anything, modify nums in-place instead
 5      */
 6     public int[] nextPermutation(int[] nums) {
 7         if (nums == null || nums.length <= 1) return nums;
 8
 9         int k = nums.length - 2;
10         // 从最右边开始,首先找到一个值而且该值比它右边那个更小,这样我们可以把该值和它右边最小的值交换。
11         // example: 1329876, the next one is 1362789
12
13         while (k >= 0 && nums[k] >= nums[k + 1]) {
14             k--;
15         }
16
17         if (k != -1) {
18             int p = nums.length - 1;
19             // we need this loop to handle the case in which duplicates exist, such as 1321
20             while (p > k) {
21                 if (nums[k] < nums[p]) {
22                     break;
23                 }
24                 p--;
25             }
26
27             swap(nums, k, nums.length - 1);
28             swapAll(nums, k + 1, nums.length - 1);
29         } else {
30             swapAll(nums, 0, nums.length - 1); // handle For [4,3,2,1], the next permutation is [1,2,3,4]
31         }
32         return nums;
33     }
34
35     public void swap(int[] nums, int i, int j) {
36         int temp = nums[i];
37         nums[i] = nums[j];
38         nums[j] = temp;
39     }
40
41     public void swapAll(int[] nums, int i, int j) {
42         while (i < j) {
43             swap(nums, i, j);
44             i++;
45             j--;
46         }
47     }
48 }

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() <= 1)
 9             return numss;
10
11         Integer[] nums = new Integer[numss.size()];
12         numss.toArray(nums);
13
14         int k = nums.length - 2;
15
16         while (k >= 0 && nums[k] <= nums[k + 1]) {
17             k--;
18         }
19         // test case 211189
20         if (k != -1) {
21             int p = nums.length -1;
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 + 1, nums.length - 1);
30         } else {
31             swapAll(nums, 0, nums.length - 1);
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 }
时间: 2024-11-10 10:39:30

Next Permutation & Previous Permutation的相关文章

leetcode_1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one-swap/ 题意:Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, t

LeetCode 1053. Previous Permutation With One Swap

原题链接在这里:https://leetcode.com/problems/previous-permutation-with-one-swap/ 题目: Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A

previous and next permutation

previous and next permutation 2763541 (找最后一个正序35) 2763541 (找3后面比3大的最后一个数4) 2764531 (交换3,4的位置) 2764135 (把4后面的5,3,1反转) 2764135 (找最后一个逆序41) 2764135 (找4后面比4小的最后一个数3) 2763145 (交换3,4的位置) 2763541 (把3后面的1,4,5反转) https://github.com/tongzhang1994/Facebook-Inte

lintcode-medium-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 permutat

Leetcode permutation 系列

关于permutation的讲解,请参见http://blog.csdn.net/xuqingict/article/details/24840183 下列题目的讲解均是基于上面的文章: 题1: Next Permutation Total Accepted: 8066 Total Submissions: 32493My Submissions Implement next permutation, which rearranges numbers into the lexicographic

lintcode 容易题:Permutation Index 排列序号

题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的.在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解. 思路: 1.对于四位数:4213 = 4*100+2

[leetcode 52] Next Permutation Report

Question: Given a list of integers, which denote a permutation. Find the next permutation in ascending order. 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] Note The list may contains duplica

uva 11129 An antiarithmetic permutation (递归)

uva 11129 An antiarithmetic permutation A permutation of n+1 is a bijective function of the initial n+1 natural numbers: 0, 1, ... n. A permutation p is called antiarithmetic if there is no subsequence of it forming an arithmetic progression of lengt

Lintcode: Previous Permuation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Note 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 permutatio