Offer prove safety [32], the minimum number of array arranged

Title Description

Enter a positive integer array, the array of all the numbers arranged in a number spliced ​​together, the splice can print out all numbers smallest one. 3,32,321 input array} {e.g., print the minimum number of three numbers can be arranged to 321,323.

A solution to a problem: DFS
1  // store traversed first cycle numbers 
2      Private  static  int [] the nums;
 . 3      // flag is traversed, traversed is 1, otherwise 0 
. 4      Private  static Integer [] Book;
 . 5      // for results to weight 
. 6      Private  static HashSet <String> Result = new new HashSet <String> ();
 . 7      public  static String PrintMinNumber ( int [] Numbers) {
 . 8          the ArrayList <String> List = new new the ArrayList <String> ();
 . 9          IF (Numbers. == 0 length ) {
 10             return  null ;
 . 11          }
 12 is          the nums = new new  int [numbers.length];
 13 is          Book = new new Integer [numbers.length];
 14          // Integer type of storage array default null, not 0, so to initialize, clear 
15          for ( int I = 0; I <book.length; I ++ ) {
 16              Book [I] = 0 ;
 . 17          }
 18 is          DFS (Numbers, 0 );
 . 19          list.addAll (Result);
 20 is          the Collections.sort (List);
 21 is          int size = list.size ();
22         for(int i=0;i<size;i++){
23             System.out.println(list.get(i));
24         }
25         return list.get(0);
26     }
27     private static void dfs(int[] arr, int step){
28         if(step==arr.length){
29             String str="";
30             for(int i=0;i<nums.length;i++){
31                 str+=nums[i];
32             }
33 is              result.add (STR);
 34 is              return ;
 35          }
 36          // iterate through the entire sequence, to try every possible 
37 [          for ( int I = 0; I <arr.length; I ++ ) {
 38 is              IF (Book [I] = 0 = ) {
 39                  the nums [STEP] = ARR [I];
 40                  Book [I] =. 1 ;
 41 is                  DFS (ARR,. 1 + STEP );
 42 is                  Book [I] = 0 ;
 43 is              }
 44 is          }
 45      }
Solution two questions: compareTo ()
1 public static String PrintMinNumber01(int [] numbers) {
2         Arrays.sort(numbers);
3         String ans = "";
4         for (int num : numbers) {
5             ans = (ans + num).compareTo(num + ans) < 0 ? (ans + num) : (num + ans);
6         }
7         return ans;
8     }
Solution three questions: Collections.sort ()
 1 public static String PrintMinNumber02(int[] numbers) {
 2         int len = numbers.length;
 3         String s = "";
 4         ArrayList<Integer> list = new ArrayList<>();
 5         for (int i = 0; i < len; i++) {
 6             list.add(numbers[i]);
 7         }
 8         // 集合类工具 Collections.sort(List<T> list,Comparator<? super T> c) 对列表进行排序
 9         Collections.sort(list, new Comparator<Integer>() {
10             @Override
11             public int compare(Integer str1, Integer str2) {
12                 String s1 = str1 + "" + str2;
13                 String s2 = str2 + "" + str1;
14                 return s1.compareTo(s2);
15             }
16         });
17         for (int j : list) {
18             s += j;
19         }
20         return s;
21     }

test:

1 public static void main(String[] args) {
2         int[] num={3,32,321};
3         String printMinNumber = PrintMinNumber02(num);
4         System.out.println(printMinNumber);
5     }
6 输出:321323

 

Guess you like

Origin www.cnblogs.com/Blog-cpc/p/12445204.html