剑指 Offer 45. 把数组排成最小的数
输入一个非负整数数组,将数组中的所有数字拼接成一个数字,然后打印所有可以连接的数字中最小的一个。
[En]
Enter an array of non-negative integers, splice all the numbers in the array into a number, and print the smallest of all the numbers that can be concatenated.
示例 1:
输入: [10,2]
输出: “102”
示例 2:
输入: [3,30,34,5,9]
输出: “3033459”
提示:
0 < nums.length <= 100< code><!--=-->

class Solution:
def minNumber(self, nums: List[int]) -> str:
def quick_sort(l, r):
if l >= r: return
i, j = l, r
while i < j:
while strs[j] + strs[l] >= strs[l] + strs[j] and i < j: j -= 1
while strs[i] + strs[l]
class Solution:
def minNumber(self, nums: List[int]) -> str:
def sort_rule(x, y):
a, b = x + y, y + x
if a > b: return 1
elif a < b: return -1
else: return 0
strs = [str(num) for num in nums]
strs.sort(key=functools.cmp_to_key(sort_rule))
return ''.join(strs)
Original: https://www.cnblogs.com/douzujun/p/16243659.html
Author: douzujun
Title: python的sort自定义排序
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7509/
转载文章受原作者版权保护。转载请注明原作者出处!