把数组排成最小的数 牛客网 剑指Offer

mac2022-06-30  66

把数组排成最小的数 牛客网 剑指Offer

题目描述输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 from functools import cmp_to_key class Solution: def PrintMinNumber(self, numbers): if numbers == None or len(numbers) <= 0: return '' strList = [] for i in numbers: strList.append(str(i)) key = cmp_to_key(lambda x, y: int(x+y)-int(y+x)) strList.sort(key=key) return ''.join(strList)

 

转载于:https://www.cnblogs.com/vercont/p/10210382.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)