LeetCode 179 Largest Number (Python)

Posted by 小明MaxMing on September 25, 2020

题目

Given a list of non negative integers, arrange them such that they form the largest number.

解题思路

将所有数字转换成字符串,ab两个数的大小为,比ab和ba的大小,将所有的数从大到小排列

代码

class Compare(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=Compare, reverse=True))
        return '0' if largest_num[0] == '0' else largest_num

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅