剑指Offer 45 把数组排成最小的数

把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

 1 # -*- coding:utf-8 -*-
 2 class LargerNum(str):        
 3     def __lt__(x,y):
 4         return x+y < y+x
 5 class Solution:
 6     def PrintMinNumber(self, numbers):
 7         if len(numbers) == 0:
 8             return ''
 9         numbers = [str(num) for num in numbers]
10         numbers.sort(key = LargerNum)
11         return '0' if numbers[0] == '0' else ''.join(numbers)
12         # write code here
原文地址:https://www.cnblogs.com/asenyang/p/11019220.html