Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 21adb38

Browse filesBrowse files
committed
feat: update leetcode solutions: No.0179. Largest Number
1 parent 2f907bf commit 21adb38
Copy full SHA for 21adb38

File tree

Expand file treeCollapse file tree

4 files changed

+64
-23
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+64
-23
lines changed
Open diff view settings
Collapse file

‎solution/0100-0199/0179.Largest Number/README.md‎

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 numberDiff line numberDiff line change
@@ -23,22 +23,44 @@
2323

2424
<!-- 这里可写通用的实现逻辑 -->
2525

26+
先转成字符串列表,再对字符串列表进行字典序降序排列。最后将列表所有字符串拼接即可。
27+
2628
<!-- tabs:start -->
2729

2830
### **Python3**
2931

3032
<!-- 这里可写当前语言的特殊实现逻辑 -->
3133

3234
```python
35+
from functools import cmp_to_key
3336

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)
3442
```
3543

3644
### **Java**
3745

3846
<!-- 这里可写当前语言的特殊实现逻辑 -->
3947

4048
```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+
}
4264
```
4365

4466
### **...**
Collapse file

‎solution/0100-0199/0179.Largest Number/README_EN.md‎

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 numberDiff line numberDiff line change
@@ -33,13 +33,33 @@
3333
### **Python3**
3434

3535
```python
36+
from functools import cmp_to_key
3637

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)
3743
```
3844

3945
### **Java**
4046

4147
```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+
}
4363
```
4464

4565
### **...**
Collapse file
+13-21Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
public class Solution {
1+
class Solution {
22
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+
}
2315
}
Collapse file
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.