Skip to content

179 Largest Number

179. Largest Number

题目: https://leetcode.com/problems/largest-number/

难度:

Medium

思路

先排序,再合并,若最后为空字符串,则返回'0'

其中排序思想为字符串的经典比较:

    """
    Replacement for built-in funciton cmp that was removed in Python 3

    Compare the two objects x and y and return an integer according to
    the outcome. The return value is negative if x < y, zero if x == y
    and strictly positive if x > y.
    """
class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        nums = [str(num) for num in nums]
        nums.sort(cmp=lambda x, y: cmp(y+x, x+y))
        return ''.join(nums).lstrip('0') if ''.join(num).lstrip('0') else '0'

或者更简单一点

class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        nums = [str(num) for num in nums]
        nums.sort(cmp=lambda x, y: cmp(y+x, x+y))
        return ''.join(nums).lstrip('0') or '0'

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组