题解 | #把数组排成最小的数#
把数组排成最小的数
https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param numbers int整型一维数组 # @return string字符串 # from functools import cmp_to_key class Solution: def PrintMinNumber(self , numbers: List[int]) -> str: # write code here # 如果字符串a拼接得到的数字大于b拼接a,那么b排在a的前面 str1 = [] for i in range(len(numbers)): str1.append(str(numbers[i])) numbers = sorted(str1,key = cmp_to_key(self.sort_max)) a= '' for num in numbers: a= a+str(num) return a def sort_max(self,a,b): if int(a+b) < int(b+a): return -1 elif int(a+b) > int(b+a): return 1 else: return 0