Largest Number

Problem: Largest Number

All we need to sort the numbers in a way that they can form the largest number. The comparison of sorting should be looked like this: if a + b is bigger than b + a, then a should be sort before b. For example, for numbers 9 and 35, we should put 9 before 35 since 935 is bigger than 359.

Code in Python:

class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        strs = [str(num) for num in nums]
        strs.sort(cmp=lambda x,y: cmp(x+y, y+x))
        return str(int(reduce(lambda x,y: x+y, strs[::-1])))

results matching ""

    No results matching ""