# 45.把数组排成最小的数

### 一、 题目

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

示例 1:

输入: \[10,2]

输出: "102"

示例 2:

输入: \[3,30,34,5,9]

输出: "3033459" &#x20;

提示:

0 < nums.length <= 100

说明:

输出结果可能非常大，所以你需要返回一个字符串而不是整数

拼接起来的数字可能会有前导 0，最后结果不需要去掉前导 0

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof>

著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

### 二、 优先队列

```
func minNumber(_ nums: [Int]) -> String {
    var queue = SwiftPriorityQueue<Int>.init(hasHigherPriority: {
        return hasHigher(x: $0, compare: $1)
    }, isEqual: {
        $0 == $1
    })
    
    var result: String = ""
    
    nums.forEach {
        queue.enqueue($0)
    }
        
    while let value = queue.dequeue() {
        result.append("\(value)")
    }
        
    return result
}

func hasHigher(x: Int, compare y: Int) -> Bool {
    let aArr = [Character](String(x))
    let bArr = [Character](String(y))
    
    let resultA = Int(String(aArr + bArr)) ?? 0
    let resultB = Int(String(bArr + aArr)) ?? 0
    
    return resultA <= resultB
}
```

![01](/files/EPzuajcK553g9RnOzsQc)

### 三、 自带的排序

```
func minNumber(_ nums: [Int]) -> String {
    let queue = nums.sorted { return hasHigher(x: $0, compare: $1) }
    var result: String = ""
    queue.forEach {
        result.append("\($0)")
    }
    return result
}

func hasHigher(x: Int, compare y: Int) -> Bool {
    let aArr = [Character](String(x))
    let bArr = [Character](String(y))
    
    let resultA = Int(String(aArr + bArr)) ?? 0
    let resultB = Int(String(bArr + aArr)) ?? 0
    
    return resultA <= resultB
}
```

![02](/files/GKpuMKIu8xV6yiD9FEtn)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/shu-ju-jie-gou-yu-suan-fa/jian-zhi-offerswift/45.-ba-shu-zu-pai-cheng-zui-xiao-de-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
