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 }