剑指offer--把数组排成最小的数

来源:互联网 发布:天际重制版捏脸数据 编辑:程序博客网 时间:2024/06/14 05:48

题目描述

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

分类:数组,字符串

解法1:将数组重新排序,然后拼接就可以了,排序的方式可以使用快速排序
比较的原则是321<3
341>3等
[java] view plain copy
  1. import java.util.ArrayList;  
  2.   
  3. public class Solution {  
  4.    public String PrintMinNumber(int [] numbers) {  
  5.         solve(numbers, 0, numbers.length-1);  
  6.         StringBuilder sb = new StringBuilder(numbers.length);  
  7.         for(int i=0;i<numbers.length;i++){  
  8.             sb.append(numbers[i]);  
  9.         }         
  10.         return sb.toString();     
  11.     }  
  12.       
  13.     public void solve(int [] numbers,int low,int high){  
  14.         if(low<high){  
  15.             int mid = partition(numbers,low,high);  
  16.             solve(numbers, low, mid-1);  
  17.             solve(numbers, mid+1, high);  
  18.         }  
  19.     }  
  20.       
  21.     public boolean compare(int x,int y){          
  22.         int count1 = 0;  
  23.         int x1 = x;  
  24.         while(x1!=0){  
  25.             count1++;  
  26.             x1/=10;  
  27.         }  
  28.         int[] arr1 = new int[count1];  
  29.         create(x,arr1);  
  30.       
  31.         int count2 = 0;  
  32.         int x2 = y;  
  33.         while(x2!=0){  
  34.             count2++;  
  35.             x2/=10;  
  36.         }  
  37.         int[] arr2 = new int[count2];  
  38.         create(y,arr2);  
  39.           
  40.         int i=0;  
  41.         while(i<count1&&i<count2){  
  42.             if(arr1[i]<arr2[i]) return true;  
  43.             else if(arr1[i]>arr2[i]) return false;  
  44.             i++;  
  45.         }  
  46.           
  47.         if(i==count1){            
  48.             int t = arr1[count1-1];           
  49.             while(i<count2&&t==arr2[i]){  
  50.                 i++;  
  51.             }  
  52.             if(i==count2 || t<arr2[i]){  
  53.                 return true;  
  54.             }else{  
  55.                 return false;  
  56.             }  
  57.         }  
  58.         if(i==count2){            
  59.             int t = arr2[count2-1];  
  60.             while(i<count1&&t==arr1[i]){  
  61.                 i++;  
  62.             }             
  63.             if(i==count1 || t<arr1[i]){  
  64.                 return false;  
  65.             }else{  
  66.                 return true;  
  67.             }  
  68.         }  
  69.         return false;  
  70.     }     
  71.       
  72.     public void create(int x,int[] arr){  
  73.         int i = arr.length-1;  
  74.         while(x!=0){              
  75.             arr[i] = x%10;  
  76.             x/=10;   
  77.             i--;  
  78.         }  
  79.     }  
  80.       
  81.     public int partition(int [] array,int low,int high){          
  82.         int temp = array[low];        
  83.         while(low<high){               
  84.             while(low<high && compare(temp,array[high])){    
  85.                 high--;                    
  86.             }                
  87.             array[low] = array[high];    
  88.             while(low<high && compare(array[low],temp)){    
  89.                 low++;    
  90.             }    
  91.             array[high] = array[low];    
  92.         }            
  93.         array[low] = temp;            
  94.         return low;    
  95.     }   
  96. }  


原文链接  http://blog.csdn.net/crazy__chen/article/details/45009813

原创粉丝点击