题解 | #把数组排成最小的数#
把数组排成最小的数
https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=265&rp=1&ru=%2Fexam%2Foj%2Fta&qru=%2Fexam%2Foj%2Fta&sourceUrl=%2Fexam%2Foj%2Fta%3FjudgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D13%26type%3D265&difficulty=&judgeStatus=3&tags=&title=&gioEnter=menu
利用了STL,思路易懂,但是如果舍弃STL实现有点难度
class Solution {
public:
string PrintMinNumber(vector<int> numbers) {
std::string res;
if (numbers.empty()) {
return res;
}
std::sort(numbers.begin(), numbers.end(), [](int a, int b) -> bool {return std::to_string(a) + std::to_string(b) < std::to_string(b) + std::to_string(a);});
for (int i = 0; i < numbers.size(); ++i) {
res += std::to_string(numbers[i]);
}
return res;
}
};