码迷,mamicode.com
首页 > 其他好文 > 详细

31. Next Permutation

时间:2017-10-20 21:52:35      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:pre   follow   for   graph   swap   ted   win   art   ogr   

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

标签:pre   follow   for   graph   swap   ted   win   art   ogr   

原文地址:http://www.cnblogs.com/huenchao/p/7701241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!