【剑指Offer】32.把数组排成最小的数(Python实现)

题目描述

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

解法一:cmp_to_key函数

# -*- coding:utf-8 -*-
class Solution:
    def PrintMinNumber(self, numbers):
        # write code here
        if not numbers:
            return ""
        if len(numbers) == 0:
            return ""
        numbers = [str(e) for e in numbers]
        numbers.sort(cmp=lambda x,y:int(x+y)-int(y+x))
        return int(''.join(numbers))

 

你可能感兴趣的:(剑指offer)