forked from xurui1995/Sword-pointing-to-offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
No33.java
56 lines (47 loc) · 1.22 KB
/
No33.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class No33 {
/**
* 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,
* 打印能拼接出所有数字中最小的一个。
* 例如输入数组{3,32,321},则打印出这3个数字能排成的最小数字321323
*/
public static void main(String[] args) {
int[] array={321,32,3};
printMin(array);
}
private static void printMin(int[] array) {
int[] clone=array.clone();
printMin(clone,0,clone.length-1);
for(int i:clone)
System.out.print(i);
}
private static void printMin(int[] array,int start,int end) {
if(start<end){
int main_number=array[end];
int small_cur=start;
for(int j=start;j<end;j++){
if(isSmall(String.valueOf(array[j]), String.valueOf(main_number))){
int temp=array[j];
array[j]=array[small_cur];
array[small_cur]=temp;
small_cur++;
}
}
array[end]=array[small_cur];
array[small_cur]=main_number;
printMin(array, 0,small_cur-1);
printMin(array, small_cur+1, end);
}
}
public static boolean isSmall(String m,String n) {
String left=m+n;
String right=n+m;
boolean result=false;
for(int i=0;i<left.length();i++){
if(left.charAt(i)<right.charAt(i))
return true;
else if(left.charAt(i)>right.charAt(i))
return false;
}
return result;
}
}