Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0179. Largest Number
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 12, 2021
1 parent 2f907bf commit 21adb38
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
24 changes: 23 additions & 1 deletion solution/0100-0199/0179.Largest Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,44 @@

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

先转成字符串列表,再对字符串列表进行字典序降序排列。最后将列表所有字符串拼接即可。

<!-- tabs:start -->

### **Python3**

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

```python
from functools import cmp_to_key

class Solution:
def largestNumber(self, nums: List[int]) -> str:
num_list = list(map(str, nums))
num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y)))
return '0' if num_list[0] == '0' else ''.join(num_list)
```

### **Java**

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

```java

class Solution {
public String largestNumber(int[] nums) {
List<String> numList = new ArrayList<>();
for (int num : nums) {
numList.add(String.valueOf(num));
}
numList.sort((a, b) -> (b + a).compareTo(a + b));
if ("0".equals(numList.get(0))) return "0";
StringBuilder sb = new StringBuilder();
for (String s : numList) {
sb.append(s);
}
return sb.toString();
}
}
```

### **...**
Expand Down
22 changes: 21 additions & 1 deletion solution/0100-0199/0179.Largest Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,33 @@
### **Python3**

```python
from functools import cmp_to_key

class Solution:
def largestNumber(self, nums: List[int]) -> str:
num_list = list(map(str, nums))
num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y)))
return '0' if num_list[0] == '0' else ''.join(num_list)
```

### **Java**

```java

class Solution {
public String largestNumber(int[] nums) {
List<String> numList = new ArrayList<>();
for (int num : nums) {
numList.add(String.valueOf(num));
}
numList.sort((a, b) -> (b + a).compareTo(a + b));
if ("0".equals(numList.get(0))) return "0";
StringBuilder sb = new StringBuilder();
for (String s : numList) {
sb.append(s);
}
return sb.toString();
}
}
```

### **...**
Expand Down
34 changes: 13 additions & 21 deletions solution/0100-0199/0179.Largest Number/Solution.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
public class Solution {
class Solution {
public String largestNumber(int[] nums) {

String[] strs = new String[nums.length];

for (int i = 0; i < strs.length; i++) {
strs[i] = nums[i] + "";
}

Arrays.sort(strs, new Comparator<String>() {

public int compare(String x, String y) {
return (y + x).compareTo(x + y);
}
});

if ("0".equals(strs[0])) {
return "0";
}

return String.join("", strs);
}
List<String> numList = new ArrayList<>();
for (int num : nums) {
numList.add(String.valueOf(num));
}
numList.sort((a, b) -> (b + a).compareTo(a + b));
if ("0".equals(numList.get(0))) return "0";
StringBuilder sb = new StringBuilder();
for (String s : numList) {
sb.append(s);
}
return sb.toString();
}
}
7 changes: 7 additions & 0 deletions solution/0100-0199/0179.Largest Number/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from functools import cmp_to_key

class Solution:
def largestNumber(self, nums: List[int]) -> str:
num_list = list(map(str, nums))
num_list.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y)))
return '0' if num_list[0] == '0' else ''.join(num_list)

0 comments on commit 21adb38

Please sign in to comment.