-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumber.java
More file actions
33 lines (27 loc) · 864 Bytes
/
LargestNumber.java
File metadata and controls
33 lines (27 loc) · 864 Bytes
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
public class LargestNumber {
public String largestNumber(int[] nums) {
NumObject[] nos = new NumObject[nums.length];
for(int i=0;i<nums.length;i++){
nos[i] = new NumObject(nums[i]);
}
Arrays.sort(nos);
String ret = "";
for(int i=0;i<nos.length;i++){
if(ret.equals("0")) ret = nos[i].num;
else ret +=nos[i].num;
}
return ret;
}
class NumObject implements Comparable<NumObject> {
String num;
public NumObject(int num) {
this.num = num+"";
}
public int compareTo(NumObject no) {
String num2 = ((NumObject)no).num;
String s12 = num+num2;
String s21 = num2+num;
return s21.compareTo(s12);
}
}
}