forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumber.py
More file actions
39 lines (29 loc) · 871 Bytes
/
LargestNumber.py
File metadata and controls
39 lines (29 loc) · 871 Bytes
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/11/12 21:03
# @Author : tc
# @File : LargestNumber.py
"""
题号 179 最大数
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入: [10,2]
输出: 210
示例 2:
输入: [3,30,34,5,9]
输出: 9534330
使用python的魔法方法
参考:https://leetcode-cn.com/problems/largest-number/solution/zi-ding-yi-pai-xu-gui-ze-by-powcai/
"""
from typing import List
class LargerNumKey(str):
def __lt__(x, y):
return x+y > y+x
class Solution:
def largestNumber(self, nums: List[int]) -> str:
largest_num = ''.join(sorted(map(str, nums), key=LargerNumKey))
return '0' if largest_num[0] == '0' else largest_num
if __name__ == '__main__':
solution = Solution()
nums = [0]
print(solution.largestNumber(nums))