MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Array/LargestNumber.py:49–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47
48"""
49class Solution(object):
50 def largestNumber(self, nums):
51 """
52 :type nums: List[int]
53 :rtype: str
54 """
55
56 if not any(nums):
57 return '0'
58
59 max_nums = len(str(max(nums)))
60
61 # 2
62 def mycmp(x, y):
63 if x + y > y + x:
64 return 1
65 else:
66 return -1
67
68 # 测试用下面的可以跑过 2 & 3。
69 def makeEqual(s, length=max_nums):
70
71 if len(s) == length:
72 return s
73 # 这种补位会通过测试,但是 Leetcode 的测试并没有包含所有的情况。
74 x = max(s) * (length - len(s))
75 return s+x
76
77 # 2
78 return ''.join(sorted(map(str, nums), cmd=mycmp, reverse=True))
79 # 3
80 return ''.join(sorted(map(str, nums), key=makeEqual, reverse=True))
81
82 # print(sorted(map(str, nums), key=makeEqual, reverse=True))
83

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected