import functools class Solution: def largestNumber(self, nums: List[int]) -> str: strnums = [str(n) for n in nums] def mycmp(a, b): if a + b > b + a: return -1 else: return 1 strnums.sort(key = functools.cmp_to_key(mycmp)) res = ''.join(strnums) if int(res) == 0: return "0" else: return res
class : def__init__(self, num): self.value = str(num) def__lt__(self, other): # '82' is before '824' because '82|824' is greater than '824|82' return self.value + other.value > other.value + self.value def__gt__(self, other): return self.value + other.value < other.value + self.value def__eq__(self, other): return self.value + other.value == other.value + self.value
classSolution: deflargestNumber(self, nums): """ :type nums: List[int] :rtype: str """ numStrings = [Comparable(n) for n in nums] numStrings.sort() output = ''.join((e.value for e in numStrings)) return output.lstrip('0') or'0'
近期评论