File tree Expand file tree Collapse file tree 4 files changed +64
-23
lines changed
solution/0100-0199/0179.Largest Number Expand file tree Collapse file tree 4 files changed +64
-23
lines changed Original file line number Diff line number Diff line change 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### ** ...**
Original file line number Diff line number Diff line change 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### ** ...**
Original file line number Diff line number Diff line change 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}
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 )
You can’t perform that action at this time.
0 commit comments