leetcode-hard-array-179 Largest Number-NO

mycode  写的很复杂,还报错。。。

参考:

class Solution:
    # @param {integer[]} nums
    # @return {string}
    def largestNumber(self, nums):
        n = len(nums)
        for i in range(n):
            for j in range(n-i-1):
                temp_1 = str(nums[j])
                temp_2 = str(nums[j+1])
                if(int(temp_1+temp_2)<int(temp_2+temp_1)):
                    temp = nums[j]
                    nums[j] = nums[j+1]
                    nums[j+1] = temp
        output = ''
        for i in nums:
            output = output + str(i)
        return str(int(output))
class Solution(object):
    def largestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        def compare(a,b):
            return int(b + a) - int(a + b)
        nums = sorted([str(x) for x in nums],cmp = compare)
        ans = ''.join(nums).lstrip('0')

        return ans or '0'
原文地址:https://www.cnblogs.com/rosyYY/p/11057435.html