面试题45. 把数组排成最小的数¶
Tags: Medium
Sort
Links: https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
示例 1:
输入: [10,2]
输出: "102"
示例 2:
输入: [3,30,34,5,9]
输出: "3033459"
提示:
0 < nums.length
<= 100
说明:
输出结果可能非常大,所以你需要返回一个字符串而不是整数 拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0
class Solution {
public:
string minNumber(vector<int>& nums) {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n = nums.size();
vector<string> tmp(n);
for (int i = 0; i < n; ++i) {
tmp[i] = to_string(nums[i]);
}
sort(tmp.begin(), tmp.end(),
[](const string & s1, const string & s2){ return s1 + s2 < s2 + s1; });
string res;
for (auto & e: tmp) res += e;
return res;
}
};