【面试题33】把数组排成最小的数

【题目】输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
【思路】将相邻两个数 3,32 转化为“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

你可能感兴趣的:(【面试题33】把数组排成最小的数)