Python solution using quicksort


  • 1
    K
    class Solution:
    # @param {integer[]} nums
    # @return {string}
    def largestNumber(self, nums):
        result = ''.join(self.quicksort(map(str, nums))).lstrip('0')
        
        if len(result) == 0:
            return '0'
        else:
            return result
        
    def quicksort(self, nums):
        if len(nums) <= 1:
            return nums
            
        i = 0
        j = len(nums) - 1
        while i < j:
            if nums[i] + nums[i+1] < nums[i+1] + nums[i]:
                nums[i], nums[i+1] = nums[i+1], nums[i]
                i += 1
            else:
                nums[i+1], nums[j] = nums[j], nums[i+1]
                j -= 1
                    
        return self.quicksort(nums[:i]) + [nums[i]] + self.quicksort(nums[i+1:])

  • 0
    W

    I think the way you implement QuickSort doesn't guarantee in place with the last line. Any way to resolve it?


Log in to reply
 

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