Python solution in one short line


  • 1
    E
    class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        return str(int(''.join(reversed(sorted([str(n) for n in nums], cmp=lambda a,b:cmp(a+b,b+a))))))
    

    To expand it in readable format, that's:

    list = [str(n) for n in nums]
    list.sort(cmp = lambda a,b : cmp(a+b, b+a) ) # a and b are strings
    list.reverse()
    s = ''.join(list)
    return str(int(s))

  • 3

    One line? Yes. Short? Mmh...

    return str(int(''.join(reversed(sorted([str(n) for n in nums], cmp=lambda a,b:cmp(a+b,b+a))))))
    return str(int(''.join(sorted([str(n) for n in nums], cmp=lambda a,b:cmp(b+a,a+b)))))
    return str(int(''.join(sorted(map(str, nums), cmp=lambda a,b:cmp(b+a,a+b)))))
    return str(int(''.join(sorted(map(str, nums), lambda a,b:cmp(b+a,a+b)))))
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.