-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path179.py
46 lines (32 loc) · 1.12 KB
/
179.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
给定一组非负整数 nums,重新排列它们每个数字的顺序(每个数字不可拆分)使之组成一个最大的整数。
注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。
示例 1:
输入:nums = [10,2]
输出:"210"
示例 2:
输入:nums = [3,30,34,5,9]
输出:"9534330"
示例 3:
输入:nums = [1]
输出:"1"
示例 4:
输入:nums = [10]
输出:"10"
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
from typing import List
import functools
class Solution:
def largestNumber(self, nums: List[int]) -> str:
def cmp(str1, str2):
return int(f"{str1}{str2}") - int(f"{str2}{str1}")
ret = "".join(sorted(map(str, nums), key=functools.cmp_to_key(cmp), reverse=True))
return ret if ret[0] != '0' else '0'
nums = [3, 30, 34, 5, 9]
print(Solution().largestNumber(nums))