题解 | #把数组排成最小的数#
把数组排成最小的数
https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993
#include <algorithm> #include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型vector * @return string字符串 */ static bool cmp(int x, int y){ string xstring = to_string(x); string ystring = to_string(y); return xstring + ystring < ystring + xstring; } string PrintMinNumber(vector<int>& numbers) { // write code here string s; sort(numbers.begin(),numbers.end(),cmp); for(auto num: numbers){ s.append(to_string(num)); } return s; } };
基于重载的排序。