剑指offer 面试题45 - 把数组排成最小的数

题目描述

面试题45. 把数组排成最小的数

解法:自定义排序规则(Python)

详细参考 Krahets

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)

注:求最大值也很好处理,就把比较规则给反过来就是

发布了189 篇原创文章 · 获赞 36 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_38204302/article/details/105610411