Though not very efficient, but intuitive and super easy to understand python o(nlogn) solution


  • 0
    S
    class Solution:
        # @param {integer[]} nums
        # @return {string}
        def largestNumber(self, nums):
            nums = map(str, nums)
            nums.sort( cmp=Solution.comparator)
            return str(int(''.join( nums)))
    
        @staticmethod
        def comparator(x,y):
            # if we have two number 34 and 35, we need to decide in which sequence these should appear in the final result hence, 
            # simple check 34 + 35 -> 3435 and 35 + 34 -> 3534, hence 3534 is greater than 3435. Thus, 35 should always appear 
            # before 34. NOTE + sign is for appending.
            if int(x+y) > int(y+x):
                return -1
            else:
                return 1
    

Log in to reply
 

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