-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
39 lines (38 loc) · 1.16 KB
/
Solution.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
public class Solution {
public String largestNumber(int[] nums) {
if (nums.length == 1) {
return String.valueOf(nums[0]);
}
int temp = 0;
for (int i = nums.length - 1; i > 0; i--) {
for (int j = 0; j < i; ++j) {
if (new Solution().compare(nums[j], nums[j + 1])) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
StringBuffer sb = new StringBuffer();
if (nums[nums.length - 1] == 0) {
return "0";
}
for (int i = nums.length - 1; i >= 0; i--) {
sb.append(String.valueOf(nums[i]));
}
return sb.toString();
}
public boolean compare(int a, int b) {
char[] ca=(String.valueOf(a)+String.valueOf(b)).toCharArray();
char[] cb=(String.valueOf(b)+String.valueOf(a)).toCharArray();
for (int i = 0; i < ca.length; i++) {
if (ca[i]>cb[i]) {
return true;
}
if (ca[i]<cb[i]) {
return false;
}
}
return false;
}
}