comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9845.%20%E6%8A%8A%E6%95%B0%E7%BB%84%E6%8E%92%E6%88%90%E6%9C%80%E5%B0%8F%E7%9A%84%E6%95%B0/README.md
面试题 45. 把数组排成最小的数
题目描述
输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
示例 1:
输入:[10,2]
输出: "102"
示例 2:
输入:[3,30,34,5,9]
输出: "3033459"
提示:
0 < nums.length <= 100
说明:
- 输出结果可能非常大,所以你需要返回一个字符串而不是整数
- 拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0
解法
方法一:自定义排序
我们将数组中的数字转换为字符串,然后按照字符串拼接的大小进行排序。具体地,比较两个字符串 a a a 和 b b b,如果 a + b < b + a a + b \lt b + a a+b<b+a,则 a a a 小于 b b b,否则 a a a 大于 b b b。
时间复杂度 O ( n × log n + n × m ) O(n \times \log n + n \times m) O(n×logn+n×m),空间复杂度 O ( n × m ) O(n \times m) O(n×m)。其中 $n $ 和 m m m 分别为数组的长度和字符串的平均长度。
Python3
from functools import cmp_to_key
class Solution:
def minNumber(self, nums: List[int]) -> str:
def cmp(a, b):
x, y = a + b, b + a
return -1 if x < y else 1
ans = [str(x) for x in nums]
ans.sort(key=cmp_to_key(cmp))
return "".join(ans)
Java
class Solution {
public String minNumber(int[] nums) {
return Arrays.stream(nums)
.mapToObj(String::valueOf)
.sorted((a, b) -> (a + b).compareTo(b + a))
.reduce((a, b) -> a + b)
.orElse("");
}
}
C++
class Solution {
public:
string minNumber(vector<int>& nums) {
vector<string> arr;
for (int& x : nums) {
arr.emplace_back(to_string(x));
}
sort(arr.begin(), arr.end(), [](const auto& a, const auto& b) {
return a + b < b + a;
});
string ans;
for (auto& x : arr) {
ans += x;
}
return ans;
}
};
Go
func minNumber(nums []int) string {
arr := []string{}
for _, x := range nums {
arr = append(arr, strconv.Itoa(x))
}
sort.Slice(arr, func(i, j int) bool { return arr[i]+arr[j] < arr[j]+arr[i] })
return strings.Join(arr, "")
}
TypeScript
function minNumber(nums: number[]): string {
return nums.sort((a, b) => Number(`${a}${b}`) - Number(`${b}${a}`)).join('');
}
Rust
impl Solution {
pub fn min_number(mut nums: Vec<i32>) -> String {
nums.sort_by(|a, b| format!("{}{}", a, b).cmp(&format!("{}{}", b, a)));
nums.iter().map(|num| num.to_string()).collect()
}
}
JavaScript
/**
* @param {number[]} nums
* @return {string}
*/
var minNumber = function (nums) {
nums.sort((a, b) => {
const x = a + '' + b;
const y = b + '' + a;
return x < y ? -1 : 1;
});
return nums.join('');
};
C#
public class Solution {
public string MinNumber(int[] nums) {
List<string> ans = new List<string>();
foreach (int x in nums) {
ans.Add(x.ToString());
}
ans.Sort((a, b) => (a + b).CompareTo(b + a));
return string.Join("", ans);
}
}
Swift
class Solution {
func minNumber(_ nums: [Int]) -> String {
let sortedNums = nums.map { String($0) }
.sorted { $0 + $1 < $1 + $0 }
return sortedNums.joined()
}
}