能想到是要根据特殊规则重新排列,python里是使用sorted(key=functools.cmp_to_key)
但是比较难想到是用字符串来进行比较,字符串比较的话还可以规避整数溢出的问题
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)