from typing import List
class Solution:
def fast_sort(self, l, r):
if l < r:
l_ = self.fast_sort_(l, r)
self.fast_sort(l, l_ - 1)
self.fast_sort(l_ + 1, r)
def fast_sort_(self, l, r):
l_, r_ = l, r
x = self.nums[l_]
while l_ < r_:
while l_ < r_ and f'{x}{self.nums[r_]}' <= f'{self.nums[r_]}{x}':
r_ -= 1
if l_ < r_:
self.nums[l_] = self.nums[r_]
l_ += 1
while l_ < r_ and f'{self.nums[l_]}{x}' <= f'{x}{self.nums[l_]}':
l_ += 1
if l_ < r_:
self.nums[r_] = self.nums[l_]
r_ -= 1
self.nums[l_] = x
return l_
def minNumber(self, nums: List[int]) -> str:
self.nums = nums
l = 0
r = len(nums) - 1
self.fast_sort(l, r)
result = ''.join(str(self.nums)[1:-1].split(', '))
return result
if __name__ == '__main__':
solution = Solution()
a = [3, 30, 34, 5, 9]
result = solution.minNumber(a)
print(result)
a = [1, 4, 7, 2, 5, 8, 0, 3, 6, 9]
result = solution.minNumber(a)
print(result)
3033459
0123456789
Process finished with exit code 0
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)