2 lines python - using map - reduce - 39 ms


  • 0
    S
    class Solution:
        # @param {integer[]} nums
        # @return {string}
        def largestNumber(self, nums):
            import functools
            return functools.reduce(lambda a,b : a+b, sorted(list(map(str,nums)),cmp=lambda a,b:1 if a+b>b+a else -1 if a+b<b+a else 0, reverse=True)) if filter(None,nums) else "0"
    

    A little longer version

    nums = sorted(map(str,nums), cmp = lambda a,b: 1 if a+b>b+a else -1 if a+b<b+a else 0, reverse=True) 
    if filter(None,nums):
      ans = reduce(lambda a,b:a+b, nums)
    else:
      ans = "0"
    return ans

Log in to reply
 

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