31. Next Permutation

Posted 暗影侠客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了31. Next Permutation相关的知识,希望对你有一定的参考价值。

31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

最简单的方式就是先全排列了,再去匹配就行~  而全排列也是典型的DFS

var a = [1,2,3,4,5];
var res = [];

function swap(a,i,j){
 var temp = a[i];
  a[i] = a[j];
  a[j] = temp;

}

function pl(a,start,res){
       a = a.slice();
     if(start>=a.length){

          res.push(a);
      }


     for(var i = start;i<a.length;i++){
           
             swap(a,start,i);
             pl(a,start+1,res);
            swap(a,start,i);
                 
      }
}

pl(a,0,res);
console.log(res);

 

其实这个问题本身是有固定套路的,直接背住下面步骤即可:

 

Now let‘s pick a number, for example, 24387651.

what is the next permutation? 24513678.

How can I get the answer?

First step: find the first ascending digit from the back of the number. 3 < 8 > 7 > 6 > 5 > 1. Then 3 is the digit.

Second step: swap that digit with the next big digit in following digits. Which one is the next big digit in 87651? 5! So swap them. Now the number becomes 24587631.

Third step: sort 87631 into 13678. The final answer is 24513678.

 

 1 /**
 2  * @param {number[]} nums
 3  * @return {void} Do not return anything, modify nums in-place instead.
 4  */
 5 var nextPermutation = function(num) {
 6     
 7       for(var i = num.length - 2; i >= 0; i--){
 8         if(num[i] < num[i + 1]){
 9             var pos;
10             var diff = Math.pow(2,31)-1;
11             for( j = i + 1; j < num.length; j++){
12                 if(num[j] > num[i] && Math.abs(num[i] - num[j]) <= diff){
13                     diff = Math.abs(num[i] - num[j]);
14                     pos = j;
15                 }
16             }
17             
18             var temp = num[i];
19             num[i] = num[pos];
20             num[pos] = temp;
21        
22             
23             for(var k =i+1,q=num.length -1;k<q;k++,q--){
24                   var temp = num[k];
25                   num[k] = num[q];
26                   num[q] = temp;
27                 
28             }
29         
30           
31             return;
32         }
33     }
34     
35     
36             for(var k = 0,q=num.length -1;k<q;k++,q--){
37                 
38                   var temp = num[k];
39                   num[k] = num[q];
40                   num[q] = temp;
41                 
42             }
43 };

 

以上是关于31. Next Permutation的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 31. Next Permutation

LeetCode - 31. Next Permutation

Python31. Next Permutation

LeetCode OJ 31. Next Permutation

LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

31. Next Permutation