[剑指Offer][数组]把数组排成最小的数
作者:互联网
题目描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。1 import java.util.ArrayList; 2 3 public class Solution { 4 public String PrintMinNumber(int [] numbers) { 5 if(numbers.length == 0) { 6 return ""; 7 } 8 adapter(numbers); 9 StringBuilder result = new StringBuilder(); 10 for(int i = 0; i < numbers.length; i ++) { 11 result.append(numbers[i]); 12 } 13 return result.toString(); 14 } 15 16 public int getMiddle(int[] data, int left, int right) { 17 int middle = data[left]; 18 while(left < right) { 19 while((left < right) && 20 (Long.valueOf(String.valueOf(middle) + String.valueOf(data[right])) <= Long.valueOf(String.valueOf(data[right]) + String.valueOf(middle)))) { 21 right --; 22 } 23 data[left] = data[right]; 24 while((left < right) && 25 (Long.valueOf(String.valueOf(middle) + String.valueOf(data[left])) >= Long.valueOf(String.valueOf(data[left]) + String.valueOf(middle)))) { 26 left ++; 27 } 28 data[right] = data[left]; 29 } 30 data[left] = middle; 31 return left; 32 } 33 34 public void quickSort(int[] data, int left, int right) { 35 if(left < right) { 36 int middle = getMiddle(data, left, right); 37 quickSort(data, left, middle - 1); 38 quickSort(data, middle + 1, right); 39 } 40 } 41 42 public void adapter(int[] data) { 43 quickSort(data, 0, data.length - 1); 44 } 45 }
标签:right,排成,Offer,int,valueOf,middle,数组,data,left 来源: https://www.cnblogs.com/StringBuilder/p/14757947.html