Largest Number
要点:这题一开始肯定想怎么track每个number从大到小的每一位,这样就把自己绕进去了。正确的方法是把找largest number组合看成几个数的排序,关键是要如何比较两个数。
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.
class Solution:
# @param {integer[]} nums
# @return {string}
def largestNumber(self, nums):
def compare(a, b):
return [1,-1][a+b>b+a]
nums = [str(a) for a in nums]
nums = sorted(nums, cmp = compare)
return ''.join(nums).lstrip('0') or '0'