本问题对应的 leetcode 原文链接:剑指 Offer 45. 把数组排成最小的数

问题描述

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

示例 1:

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

示例 2:

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

提示:

  • 0 < nums.length <= 100

说明:

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

解题思路

视频讲解直达: 本题视频讲解

代码实现

class Solution {
 public String minNumber(int[] nums) {

        String[] strs = new String[nums.length];

        for (int i = 0; i < nums.length; i++) {
            strs[i] = String.valueOf(nums[i]);
        }

        quickSort(strs, 0, strs.length - 1);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            sb.append(strs[i]);
        }
        return sb.toString();
    }
///时间nlogn,空间 On
    // 快速排序模版(和普通快速排序模版一样的,除了比较那里不一样)
    private void quickSort(String[] arr, int left, int right) {
        if (left > right) {
            return;
        }
        int i = partition(arr, left, right);
        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);

    }

    int partition(String[] arr, int left, int right){
       String pivot = arr[left]; // 中间值
        int i = left;
        int j = right;

        while (i < j) {
            while (i <= j && (arr[i] + pivot).compareTo(pivot + arr[i]) <= 0) {
                i++;
            }

            while (i <= j && (arr[j] + pivot).compareTo(pivot + arr[j]) >= 0) {
                j--;
            }
            if(i >= j){
                break;
            }
            String temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        arr[left] = arr[j];
        arr[j] = pivot;

        return j;

    }
}

时间复杂度:O(nlogn)
额外空间复杂度:O(n)

Python

class Solution(object):
    def minNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        def quickSort(arr, left, right):
            if left > right:
                return
            i = partition(arr, left, right)
            quickSort(arr, left, i - 1)
            quickSort(arr, i + 1, right)

        def partition(arr, left, right):
            pivot = arr[left]
            i = left
            j = right

            while i < j:
                while i <= j and (arr[i] + pivot) <= (pivot + arr[i]):
                    i += 1

                while i <= j and (arr[j] + pivot) >= (pivot + arr[j]):
                    j -= 1

                if i >= j:
                    break

                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp

            arr[left] = arr[j]
            arr[j] = pivot

            return j

        nums = [str(num) for num in nums]
        quickSort(nums, 0, len(nums)-1)

        return "".join(nums)

C++

class Solution {
public:
    string minNumber(vector<int>& nums) {
        vector<string> strs;
        for (int i = 0; i < nums.size(); i++) {
            strs.push_back(to_string(nums[i]));
        }

        quickSort(strs, 0, strs.size() - 1);

        string res;
        for (int i = 0; i < strs.size(); i++) {
            res += strs[i];
        }
        return res;
    }

private:
    // 快速排序模版(和普通快速排序模版一样的,除了比较那里不一样)
    void quickSort(vector<string>& arr, int left, int right) {
        if (left > right) {
            return;
        }
        int i = partition(arr, left, right);
        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);
    }

    int partition(vector<string>& arr, int left, int right) {
        string pivot = arr[left];
        int i = left;
        int j = right;

        while (i < j) {
            while (i <= j && (arr[i] + pivot) <= (pivot + arr[i])) {
                i++;
            }

            while (i <= j && (arr[j] + pivot) >= (pivot + arr[j])) {
                j--;
            }

            if (i >= j) {
                break;
            }

            string temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        arr[left] = arr[j];
        arr[j] = pivot;
        return j;
    }
};

Go

func minNumber(nums []int) string {
    var strs []string
    for _, num := range nums {
        strs = append(strs, strconv.Itoa(num))
    }

    quickSort(&strs, 0, len(strs)-1)

    var res string
    for _, str := range strs {
        res += str
    }

    return res
}

func quickSort(arr *[]string, left, right int) {
    if left > right {
        return
    }

    pivot := (*arr)[left]
    i := left
    j := right

    for i < j {
        for i <= j && ((*arr)[i]+pivot) <= (pivot+(*arr)[i]) {
            i++
        }

        for i <= j && ((*arr)[j]+pivot) >= (pivot+(*arr)[j]) {
            j--
        }

        if i >= j {
            break
        }

        (*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
    }

    (*arr)[left], (*arr)[j] = (*arr)[j], pivot
    quickSort(arr, left, j-1)
    quickSort(arr, j+1, right)
}

JS

/**
 * @param {number[]} nums
 * @return {string}
 */
var minNumber = function(nums) {
    let strs = nums.map(num => String(num));

    quickSort(strs, 0, strs.length - 1);

    let sb = '';
    for (let i = 0; i < strs.length; i++) {
        sb += strs[i];
    }
    return sb;

    // 快速排序模版(和普通快速排序模版一样的,除了比较那里不一样)
    function quickSort(arr, left, right) {
        if (left > right) {
            return;
        }
        let i = partition(arr, left, right);
        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);
    }

    function partition(arr, left, right) {
        const pivot = arr[left]; // 中间值
        let i = left;
        let j = right;

        while (i < j) {
            while (i <= j && (arr[i] + pivot) <= (pivot + arr[i])) {
                i++;
            }

            while (i <= j && (arr[j] + pivot) >= (pivot + arr[j])) {
                j--;
            }
            if(i >= j){
                break;
            }
            const temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        arr[left] = arr[j];
        arr[j] = pivot;

        return j;
    }
};

发表回复

后才能评论