• 一道题引发的排序思考——把数组排成最小的数


    刷题刷到一道觉得特有价值的排序 题,今天和大家分享一下:

    题目描述:

    输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。

    示例 1:

    输入: [10,2]
    输出: "102"

    示例 2:

    输入: [3,30,34,5,9]
    输出: "3033459"

    说明:

    • 输出结果可能非常大,所以你需要返回一个字符串而不是整数
    • 拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0

    思路(LeetCode):

    此题求拼接起来的 “最小数字” ,本质上是一个排序问题。
    排序判断规则: 设 nums 任意两数字的字符串格式 x 和 y ,则

    • 若拼接字符串 x + y > y + x,x+y>y+x ,则 x > y;
    • 反之,若 x + y < y + x,x+y<y+x ,则 x < y ;

    根据以上规则,套用任何排序方法对 numsnums 执行排序即可。

     算法流程:

    • 初始化: 字符串列表 strsstrs ,保存各数字的字符串格式;
    • 列表排序: 应用以上 “排序判断规则” ,对 strsstrs 执行排序;
    • 返回值: 拼接 strs 中的所有字符串,并返回。

    复杂度分析:

    时间复杂度 O(NlogN) : N 为最终返回值的字符数量( strs列表的长度 ≤N );使用快排内置函数的平均时间复杂度为O(NlogN) ,最差为 O(N^2) 。
    空间复杂度 O(N) : 字符串列表 strs占用线性大小的额外空间。

    代码实现:

    一、冒泡

    看懂思路,写代码还是非常简单的。首先我先写一下比较low的冒泡排序来做(为什么用这么low的?因为就冒泡背的最熟hhhh)

    nums = [3,5,9,30,34]
    def bubblesort(num):
        num = list(map(str,num))
        for i in range(len(num)-1):
            for j in range(len(num)-i-1):
                if num[j] + num[j+1] > num[j+1] + num[j]:
                    num[j],num[j+1] = num[j+1],num[j]
        return "".join(num)
    print(bubblesort(nums))

    二、快排 

    但是冒泡的时间复杂度比较高O(N^2),所以我们可以用快排来实现:

    首先为了直观,我在这里先用写一个快排的简单排序代码,如下:

    # 快速排序
    nums = [3,5,9,30,34]
    
    def fsort(nums,l,r):
        if l >= r:
            return
        i, j = l, r
        while i < j:
            while nums[j] >= nums[l] and i < j:
                j -= 1
            while nums[i] <= nums[l] and i < j:
                i += 1
            nums[i], nums[j] = nums[j], nums[i]
        nums[i], nums[l] = nums[l], nums[i]
        fsort(nums,l, i - 1)
        fsort(nums,i + 1, r)
        return nums
    print(fsort(nums,0,len(nums)-1))

    接下来我们就用快排来解题,思路是一样的,只需要把排序规则那里做一下改动即可

    nums = [3,30,34,5,9]
    def minNumber(nums):
        def fast_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]
            fast_sort(l, i - 1)
            fast_sort(i + 1, r)
    
        strs = [str(num) for num in nums]
        fast_sort(0, len(strs) - 1)
        return ''.join(strs)
    
    print(minNumber(nums))

     三、python内置排序函数

    在这里呢我先将代码写在这里:

    nums = [3,30,34,5,9]
    import functools
    def minNumber(nums):
            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)
    
    print(minNumber(nums))

    在这里用到了python内置的排序函数sort,它的排序规则用函数sort_rule来完成。

    为了更直观一点,我在这里写一个简单的排序代码,供大家参考:

    #内置函数完成排序
    nums = [3,30,34,5,9]
    def sort_rule(x, y):
        a, b = x, y
        if a > b:
            return 1
        elif a < b:
            return -1
        else:
            return 0
    
    nums.sort(key = functools.cmp_to_key(sort_rule))
    print(nums)
  • 相关阅读:
    Codeforces 120F Spiders
    Codeforces 509C Sums of Digits
    Topcoder SRM 497 DIV2 1000 MakeSquare
    codeforces 22B Bargaining Table
    Codeforces 487B Strip
    Codeforces 132C Logo Turtle
    关闭窗口对话框提示 messagedlg应用和showmodal的使用
    如何让窗口显示在电脑屏幕中间
    delphi项目程序输出编译成应用程序文件
    delphi程序项目创建和保存
  • 原文地址:https://www.cnblogs.com/sunny0824/p/13647058.html
Copyright © 2020-2023  润新知