题目
代码
参考k神
执行用时:36 ms, 在所有 Python3 提交中击败了87.59% 的用户
内存消耗:15.2 MB, 在所有 Python3 提交中击败了25.01% 的用户
通过测试用例:222 / 222
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] <= strs[l] + strs[i] and i < j: i += 1
strs[i], strs[j] = strs[j], strs[i]
strs[i], strs[l] = strs[l], strs[i]
quick_sort(l, i - 1)
quick_sort(i + 1, r)
strs = [str(num) for num in nums]
quick_sort(0, len(strs) - 1)
return ''.join(strs)
【方法2】内置排序
执行用时:40 ms, 在所有 Python3 提交中击败了71.31% 的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了66.41% 的用户
通过测试用例:222 / 222
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)

本文探讨了一种使用快速排序和内置排序方法解决给定整数列表转为字符串并求最小数的问题。两种实现方式对比了执行时间和内存消耗,展示了在特定场景下的性能差异。
931

被折叠的 条评论
为什么被折叠?



