Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8eed51

Browse files
committed
179_Largest_Number
1 parent 0536fee commit d8eed51

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Also, there are open source implementations for basic data structs and algorithm
7474
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/166_Fraction_to_Recurring_Decimal.py) | % and Hash to find duplicate |
7575
| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/167_Two_Sum_II_Input_array_is_sorted.py) | Two points O(n) and O(1) |
7676
| 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/170_Two_Sum_III-Data_structure_design.py) | 1. Hash, O(1) for add, O(n) for find, O(n) space<br>2. sorted list, O(logn) for add, O(n) for find, O(n) space<br> 3. Sort before find, O(1) for add, O(nlogn) for find, O(n) space|
77+
| 179 | [Largest Number](https://leetcode.com/problems/largest-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/179_Largest_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/179_Largest_Number.java) | Define a comparator with str(x) + str(y) > str(y) + str(x), O(nlgn) and O(n) |
7778
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
7879
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
7980
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |

java/179_Largest_Number.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
private class LargerNumberComparator implements Comparator<String> {
3+
@Override
4+
public int compare(String a, String b) {
5+
String order1 = a + b;
6+
String order2 = b + a;
7+
return order2.compareTo(order1);
8+
}
9+
}
10+
11+
public String largestNumber(int[] nums) {
12+
// Get input integers as strings.
13+
String[] asStrs = new String[nums.length];
14+
for (int i = 0; i < nums.length; i++) {
15+
asStrs[i] = String.valueOf(nums[i]);
16+
}
17+
18+
// Sort strings according to custom comparator.
19+
Arrays.sort(asStrs, new LargerNumberComparator());
20+
21+
// If, after being sorted, the largest number is `0`, the entire number
22+
// is zero.
23+
if (asStrs[0].equals("0")) {
24+
return "0";
25+
}
26+
27+
// Build largest number from sorted array.
28+
String largestNumberStr = new String();
29+
for (String numAsStr : asStrs) {
30+
largestNumberStr += numAsStr;
31+
}
32+
33+
return largestNumberStr;
34+
}
35+
}

python/179_Largest_Number.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class LargerNumKey(str):
2+
def __lt__(x, y):
3+
return x + y > y + x
4+
5+
6+
class Solution:
7+
def largestNumber(self, nums):
8+
largest_num = ''.join(sorted(map(str, nums), key=LargerNumKey))
9+
return '0' if largest_num[0] == '0' else largest_num

0 commit comments

Comments
 (0)