GeezerVison is showing github.com — feat: update leetcode solutions: No.0179. Largest Number · zuofang/leetcode@21adb38
File tree
Expand file treeCollapse file tree
Open diff view settings
solution/0100-0199/0179.Largest Number
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: solution/0100-0199/0179.Largest Number/README.md +23-1Lines changed: 23 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
23 | 23 |
|
24 | 24 | <!-- 这里可写通用的实现逻辑 --> |
25 | 25 |
|
| 26 | +先转成字符串列表,再对字符串列表进行字典序降序排列。最后将列表所有字符串拼接即可。 |
| 27 | + |
26 | 28 | <!-- tabs:start --> |
27 | 29 |
|
28 | 30 | ### **Python3** |
29 | 31 |
|
30 | 32 | <!-- 这里可写当前语言的特殊实现逻辑 --> |
31 | 33 |
|
32 | 34 | ```python |
| 35 | +from functools import cmp_to_key |
33 | 36 |
|
| 37 | +class Solution: |
| 38 | + def largestNumber(self, nums: List[int]) -> str: |
| 39 | + num_list = list(map(str, nums)) |
| 40 | + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) |
| 41 | + return '0' if num_list[0] == '0' else ''.join(num_list) |
34 | 42 | ``` |
35 | 43 |
|
36 | 44 | ### **Java** |
37 | 45 |
|
38 | 46 | <!-- 这里可写当前语言的特殊实现逻辑 --> |
39 | 47 |
|
40 | 48 | ```java |
41 | | - |
| 49 | +class Solution { |
| 50 | + public String largestNumber(int[] nums) { |
| 51 | + List<String> numList = new ArrayList<>(); |
| 52 | + for (int num : nums) { |
| 53 | + numList.add(String.valueOf(num)); |
| 54 | + } |
| 55 | + numList.sort((a, b) -> (b + a).compareTo(a + b)); |
| 56 | + if ("0".equals(numList.get(0))) return "0"; |
| 57 | + StringBuilder sb = new StringBuilder(); |
| 58 | + for (String s : numList) { |
| 59 | + sb.append(s); |
| 60 | + } |
| 61 | + return sb.toString(); |
| 62 | + } |
| 63 | +} |
42 | 64 | ``` |
43 | 65 |
|
44 | 66 | ### **...** |
|
Collapse file
Copy file name to clipboardExpand all lines: solution/0100-0199/0179.Largest Number/README_EN.md +21-1Lines changed: 21 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
33 | 33 | ### **Python3** |
34 | 34 |
|
35 | 35 | ```python |
| 36 | +from functools import cmp_to_key |
36 | 37 |
|
| 38 | +class Solution: |
| 39 | + def largestNumber(self, nums: List[int]) -> str: |
| 40 | + num_list = list(map(str, nums)) |
| 41 | + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) |
| 42 | + return '0' if num_list[0] == '0' else ''.join(num_list) |
37 | 43 | ``` |
38 | 44 |
|
39 | 45 | ### **Java** |
40 | 46 |
|
41 | 47 | ```java |
42 | | - |
| 48 | +class Solution { |
| 49 | + public String largestNumber(int[] nums) { |
| 50 | + List<String> numList = new ArrayList<>(); |
| 51 | + for (int num : nums) { |
| 52 | + numList.add(String.valueOf(num)); |
| 53 | + } |
| 54 | + numList.sort((a, b) -> (b + a).compareTo(a + b)); |
| 55 | + if ("0".equals(numList.get(0))) return "0"; |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + for (String s : numList) { |
| 58 | + sb.append(s); |
| 59 | + } |
| 60 | + return sb.toString(); |
| 61 | + } |
| 62 | +} |
43 | 63 | ``` |
44 | 64 |
|
45 | 65 | ### **...** |
|
Collapse file
Copy file name to clipboard +13-21Lines changed: 13 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | | -public class Solution { |
| 1 | +class Solution { |
2 | 2 | public String largestNumber(int[] nums) { |
3 | | - |
4 | | - String[] strs = new String[nums.length]; |
5 | | - |
6 | | - for (int i = 0; i < strs.length; i++) { |
7 | | - strs[i] = nums[i] + ""; |
8 | | - } |
9 | | - |
10 | | - Arrays.sort(strs, new Comparator<String>() { |
11 | | - |
12 | | - public int compare(String x, String y) { |
13 | | - return (y + x).compareTo(x + y); |
14 | | - } |
15 | | - }); |
16 | | - |
17 | | - if ("0".equals(strs[0])) { |
18 | | - return "0"; |
19 | | - } |
20 | | - |
21 | | - return String.join("", strs); |
22 | | - } |
| 3 | + List<String> numList = new ArrayList<>(); |
| 4 | + for (int num : nums) { |
| 5 | + numList.add(String.valueOf(num)); |
| 6 | + } |
| 7 | + numList.sort((a, b) -> (b + a).compareTo(a + b)); |
| 8 | + if ("0".equals(numList.get(0))) return "0"; |
| 9 | + StringBuilder sb = new StringBuilder(); |
| 10 | + for (String s : numList) { |
| 11 | + sb.append(s); |
| 12 | + } |
| 13 | + return sb.toString(); |
| 14 | + } |
23 | 15 | } |
Collapse file
Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +from functools import cmp_to_key |
| 2 | + |
| 3 | +class Solution: |
| 4 | + def largestNumber(self, nums: List[int]) -> str: |
| 5 | + num_list = list(map(str, nums)) |
| 6 | + num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y))) |
| 7 | + return '0' if num_list[0] == '0' else ''.join(num_list) |