【題目】輸入一個(gè)正整數(shù)數(shù)組,把數(shù)組里所有數(shù)字拼接起來(lái)排成一個(gè)數(shù),打印能拼接出的所有數(shù)字中最小的一個(gè)。例如輸入數(shù)組{3,32,321},則打印出這三個(gè)數(shù)字能排成的最小數(shù)字為321323。
【思路】將相鄰兩個(gè)數(shù) 3,32 轉(zhuǎn)化為“332” “323”比較
【代碼】
class Solution:
def PrintMinNumber(self, numbers):
# write code here
if not numbers:
return ""
numstr = map(str,numbers)
l = lambda n1,n2:int(n1+n2)-int(n2+n1)
numsort = sorted(numstr,cmp=l)
res = "".join(i for i in numsort)
return res