Simple solution with custom compare function and bubble sort


  • 0
    V
    class Solution(object):
        def largestNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: str
            """
            flag = True
            for i in nums:
                if i!=0:
                    flag = False
                    break
            
            if flag == True: return "0"
            
            def compare(i,j):
                if(int(str(i)+str(j)) > int(str(j)+str(i))):
                    return 1
                else: 
                    return -1
            
            def swap(nums, i, j):
                temp = nums[i]
                nums[i]=nums[j]
                nums[j]=temp
            
            ## bubble sorting
            for i in range(0, len(nums), 1):
                for j in range(len(nums)-1, i, -1):
                    if(compare(nums[i],nums[j])!=1):
                        swap(nums,i,j)
            
            ## creating string using concatenation               
            string1 = ""
            for i in nums:
                string1 = string1 + str(i)
            return string1

Log in to reply
 

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