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.
对一组数字排序,使其组成的数字最大
思路:排序,不过重新定义排序规则,比如字符串x和字符串y,比较x+y和y+x的大小,如果x+y>y+x,则x>y
1 class Solution: 2 def largestNumber(self, nums): 3 nums = [str(n) for n in nums] 4 nums.sort(cmp=lambda y,x:cmp(x+y,y+x)) 5 return ‘‘.join(nums).lstrip(‘0‘) or ‘0‘
时间: 2024-10-13 03:45:29