forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumber.java
More file actions
87 lines (67 loc) · 2.13 KB
/
LargestNumber.java
File metadata and controls
87 lines (67 loc) · 2.13 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Algorithms.string;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class LargestNumber {
public static void main(String[] strs) {
int[] num = {1, 2};
largestNumber(num);
ArrayList<Card> cards = new ArrayList<Card>(5);
Card card1 = new Card(1);
Card card2 = new Card(4);
Card card3 = new Card(2);
cards.add(card1);
cards.add(card2);
cards.add(card3);
if (card1.compareTo(card2) > 0) {
System.out.println("true");
} else {
System.out.println("false");
}
// uses compareTo method implemented in Card object to compare them
Collections.sort(cards);
// uses compare method implements in Comparator class
//Collections.sort(cards, new CompareBySuitRank());
for (Card card : cards)
System.out.println(card.toString());
}
public static class Card implements Comparable<Card>{
int val;
public String toString() {
return "" + val;
}
public Card(int val) {
super();
this.val = val;
}
public int compareTo(Card o) {
return this.val - o.val;
}
}
public static String largestNumber(int[] num) {
// 1045
// 1111 begin.
if (num == null) {
return null;
}
ArrayList<Integer> list = new ArrayList<Integer>();
for (int n1: num) {
list.add(n1);
}
Collections.sort(list, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2) {
String s1 = "" + o1 + o2;
String s2 = "" + o2 + o1;
return s2.compareTo(s1);
}
});
StringBuilder sb = new StringBuilder();
for (int n: list) {
sb.append(n);
}
if (sb.charAt(0) == '0') {
return "0";
}
return sb.toString();
}
}