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
[1,3,2,3]for, the next permutation is[1,3,3,2]
[4,3,2,1]for, 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 are less than it right Neighbor. And then switch it with the smallest number on its right side, but that smallest number must is greater than the number to Be switched.
1 Public classSolution {2 /**3 * @param nums:an array of integers4 * @return: Return Nothing (void), does not return anything, modify Nums in-place instead5 */6 Public int[] Nextpermutation (int[] nums) {7 if(Nums = =NULL|| Nums.length <=1)returnnums;8 9 intK = Nums.length-2;Ten //starting at the far right, a value is found first and the value is smaller than the one on the right, so we can swap the value with the smallest value on its right. One //example:1329876, the next one is 1362789 A - while(k >=0&& Nums[k] >= nums[k +1]) { -k--; the } - - if(k! =-1) { - intp = nums.length-1; + //We need this loop to handle the case in which duplicates exist, such as 1321 - while(P >k) { + if(Nums[k] <Nums[p]) { A Break; at } -p--; - } - -Swap (Nums, K, Nums.length-1); -Swapall (Nums, k +1, Nums.length-1); in}Else { -Swapall (Nums,0, Nums.length-1);//handle for [4,3,2,1], the next permutation is [1,2,3,4] to } + returnnums; - } the * Public voidSwapint[] Nums,intIintj) { $ inttemp =Nums[i];Panax NotoginsengNums[i] =Nums[j]; -NUMS[J] =temp; the } + A Public voidSwapall (int[] Nums,intIintj) { the while(I <j) { + swap (Nums, I, j); -i++; $j--; $ } - } -}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
[1,3,2,3]for, the previous permutation is[1,2,3,3]
[1,2,3,4]for, the previous permutation is[4,3,2,1]
Analysis:
From the right most, find a number which are greater than its right neighbor and then switch it with the largest number on its Right side, but that largest number must is less than the number to be switched.
1 Public classSolution {2 /**3 * @param nums:a list of integers4 * @return: A list of integers that ' s previous permuation5 */6 PublicArraylist<integer> Previouspermuation (arraylist<integer>NUMSS) {7 8 if(Numss = =NULL|| Numss.size () <=1)9 returnNumss;Ten Oneinteger[] Nums =Newinteger[numss.size ()]; A Numss.toarray (nums); - - intK = Nums.length-2; the - while(k >=0&& Nums[k] <= nums[k +1]) { -k--; - } + //test Case 211189 - if(k! =-1) { + intp = nums.length-1; A while(P >k) { at if(Nums[k] >Nums[p]) { - Swap (Nums, K, p); - Break; - } -p--; - } inSwapall (Nums, k +1, Nums.length-1); -}Else { toSwapall (Nums,0, Nums.length-1); + } - return NewArraylist<integer>(Arrays.aslist (nums)); the } * $ Public voidSwap (integer[] nums,intIintj) {Panax Notoginseng inttemp =Nums[i]; -Nums[i] =Nums[j]; theNUMS[J] =temp; + } A the Public voidSwapall (integer[] nums,intIintj) { + while(I <j) { - swap (Nums, I, j); $i++; $j--; - } - } the}
Next Permutation & Previous permutation