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

 

 
posted @ 2016-07-15 11:52  北叶青藤  阅读(263)  评论(0编辑  收藏  举报