mirror of https://github.com/doocs/leetcode.git
feat: update leetcode solutions: No.0179. Largest Number
This commit is contained in:
parent
2f907bf678
commit
21adb38ba1
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
先转成字符串列表,再对字符串列表进行字典序降序排列。最后将列表所有字符串拼接即可。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
|
@ -30,7 +32,13 @@
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```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**
|
||||
|
|
@ -38,7 +46,21 @@
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```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();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in New Issue