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(object):
def myCompare(self, s1, s2):
n1 = int(s1+s2)
n2 = int(s2+s1)
if n1 < n2:
return 1
elif n1 == n2:
return 0
else:
return -1
def largestNumber(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
strList = [str(x) for x in nums]
strList.sort(cmp = self.myCompare)
while len(strList) > 1 and strList[0] == '0':
strList = strList[1:]
return "".join(strList)

本文介绍了一个算法,用于将一组非负整数排列成最大的数值字符串。通过比较自定义的字符串组合方式,实现从给定数组中形成最大可能的数字。
844

被折叠的 条评论
为什么被折叠?



