The whole arrangement and application

In general all-digital arrangement for scheduling problems, such as seeking greater than 123,567 1,2,3,5,6,7 these first number of digits, i.e. 123,576, this time can be used to full array do it all possible are ranked, and then find the minimum number of greater than 123,567, that is, we ask the. The time complexity is O (n!). Of course, there are other approaches to this problem, the time complexity is O (n); in need can find me two days to write. The logic is not complicated.

Another example of a recent Alibaba question: the input digital 6 0-9, the maximum period of time to find out a combination of the minimum time, if there is no output N / A. 123456 example, the maximum time is: 23: 56: 41, the minimum time is 12:34:56. First look at the whole arrangement, then first determine whether the time format for each requirement. 00:00:00 then set the maximum, minimum is: 23: 59: 59, then to compare like, it is not necessary to go over in terms of seconds, the direct string comparison on the Ok.

We can see the whole arrangement at this time is simple and crude, but another example: 1,2,2,3,4, if the arrangement of how many different sequences, taking into account the two 2 here, so first of all is to Sort, the next is the middle of the details of the deal, the time code is the code I try.

Full permutation code is as follows:

  1. public class  permutate {   
  2.     public static int  Total = 0;    
  3.     public static void swap(String[] str, int i, int j)  
  4.     {  
  5.         String temp = new String();  
  6.         temp = str[i];  
  7.         str[i] = str[j];  
  8.         str[j] = temp;  
  9.     }  
  10.     public static void arrange (String[] str, int st, int len)  
  11.     {  
  12.         if (st == len - 1)  
  13.         {  
  14.             for (int i = 0; i < len; i ++)  
  15.             {  
  16.                 System.out.print(str[i]+ "  ");  
  17.             }  
  18.             System.out.println();  
  19.             total++;  
  20.         }  
  21.         else  
  22.         {  
  23.             for (int i = st; i < len; i ++)  
  24.             {  
  25.                 swap(str, st, i);  
  26.                 arrange(str, st + 1, len);  
  27.                 swap(str, st, i);  
  28.             }  
  29.         }     
  30.     }  
  31.     public static void main(String[] args) {  
  32.          String str[] = {"a","b","c"};  
  33.          arrange(str, 0, str.length);  
  34.          System.out.println(total);  
  35.     }  
  36. }  


Guess you like

Origin blog.csdn.net/mad_sword/article/details/79634093