Skip to content

Commit d263652

Browse files
refactor 179
1 parent 02dc64b commit d263652

File tree

1 file changed

+68
-56
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+68
-56
lines changed

src/main/java/com/fishercoder/solutions/_179.java

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,86 @@
44
import java.util.Comparator;
55

66
/**
7-
* Given a list of non negative integers, arrange them such that they form the largest number.
7+
* 179. Largest Number
88
9-
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
9+
Given a list of non negative integers, arrange them such that they form the largest number.
10+
11+
Example 1:
12+
13+
Input: [10,2]
14+
Output: "210"
15+
16+
Example 2:
17+
18+
Input: [3,30,34,5,9]
19+
Output: "9534330"
1020
1121
Note: The result may be very large, so you need to return a string instead of an integer.
1222
*/
1323
public class _179 {
1424

25+
public static class Solution1 {
1526
public String largestNumber(int[] num) {
16-
if (num.length == 0) {
17-
return "";
27+
if (num.length == 0) {
28+
return "";
29+
}
30+
if (num.length == 1) {
31+
return Integer.toString(num[0]);
32+
}
33+
String[] str = new String[num.length];
34+
for (int i = 0; i < num.length; i++) {
35+
str[i] = Integer.toString(num[i]);
36+
}
37+
Arrays.sort(str, new StringComparator());
38+
StringBuilder sb = new StringBuilder("");
39+
for (int i = num.length - 1; i >= 0; i--) {
40+
sb.append(str[i]);
41+
}
42+
if (sb.charAt(0) == '0') {
43+
return "0";
44+
}
45+
return sb.toString();
46+
}
47+
48+
class StringComparator implements Comparator<String> {
49+
public int compare(String s1, String s2) {
50+
if (s1.length() == 0 && s2.length() == 0) {
51+
return 0;
1852
}
19-
if (num.length == 1) {
20-
return Integer.toString(num[0]);
53+
if (s2.length() == 0) {
54+
return 1;
2155
}
22-
String[] str = new String[num.length];
23-
for (int i = 0; i < num.length; i++) {
24-
str[i] = Integer.toString(num[i]);
56+
if (s1.length() == 0) {
57+
return -1;
2558
}
26-
Arrays.sort(str, new StringComparator());
27-
StringBuilder sb = new StringBuilder("");
28-
for (int i = num.length - 1; i >= 0; i--) {
29-
sb.append(str[i]);
59+
for (int i = 0; i < s1.length() && i < s2.length(); i++) {
60+
if (s1.charAt(i) > s2.charAt(i)) {
61+
return 1;
62+
} else if (s1.charAt(i) < s2.charAt(i)) {
63+
return -1;
64+
}
3065
}
31-
if (sb.charAt(0) == '0') {
32-
return "0";
66+
if (s1.length() == s2.length()) {
67+
return 0;
3368
}
34-
return sb.toString();
35-
}
36-
37-
class StringComparator implements Comparator<String> {
38-
public int compare(String s1, String s2) {
39-
if (s1.length() == 0 && s2.length() == 0) {
40-
return 0;
41-
}
42-
if (s2.length() == 0) {
43-
return 1;
44-
}
45-
if (s1.length() == 0) {
46-
return -1;
47-
}
48-
for (int i = 0; i < s1.length() && i < s2.length(); i++) {
49-
if (s1.charAt(i) > s2.charAt(i)) {
50-
return 1;
51-
} else if (s1.charAt(i) < s2.charAt(i)) {
52-
return -1;
53-
}
54-
}
55-
if (s1.length() == s2.length()) {
56-
return 0;
57-
}
58-
if (s1.length() > s2.length()) {
59-
if (s1.charAt(0) < s1.charAt(s2.length())) {
60-
return 1;
61-
} else if (s1.charAt(0) > s1.charAt(s2.length())) {
62-
return -1;
63-
} else {
64-
return compare(s1.substring(s2.length()), s2);
65-
}
66-
} else {
67-
if (s2.charAt(0) < s2.charAt(s1.length())) {
68-
return -1;
69-
} else if (s2.charAt(0) > s2.charAt(s1.length())) {
70-
return 1;
71-
} else {
72-
return compare(s1, s2.substring(s1.length()));
73-
}
74-
}
69+
if (s1.length() > s2.length()) {
70+
if (s1.charAt(0) < s1.charAt(s2.length())) {
71+
return 1;
72+
} else if (s1.charAt(0) > s1.charAt(s2.length())) {
73+
return -1;
74+
} else {
75+
return compare(s1.substring(s2.length()), s2);
76+
}
77+
} else {
78+
if (s2.charAt(0) < s2.charAt(s1.length())) {
79+
return -1;
80+
} else if (s2.charAt(0) > s2.charAt(s1.length())) {
81+
return 1;
82+
} else {
83+
return compare(s1, s2.substring(s1.length()));
84+
}
7585
}
86+
}
7687
}
88+
}
7789
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy