최대 수량 | Letcode 25일차

675 단어 python

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


예제 - """
입력: [10,2]
산출:"210"
"""
솔루션 -
import functools
class Solution:
    def largestNumber(self, nums: List[int]) -> str:
        def sorted_by(a, b):
            if a+b > b+a:
                return 1
            elif a+b < b+a:
                return -1
            else:
                return 0
        cmp = functools.cmp_to_key(sorted_by)
        return "".join(sorted([str(x) for x in nums], key=cmp, reverse=True)).lstrip('0') or '0'

좋은 웹페이지 즐겨찾기