题目描述
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
思路:
对数组进行排序,对于每两个数字a,b
若a 拼 b < b 拼 a
则a , b
否则 b , a
如 a=34 b=2,
342 > 234
则排b , a
class Solution
{
public:
static bool cmp(
int a,
int b)
{
string A=
"";
string B=
"";
A+=
to_string(a);
A+=
to_string(b);
B+=
to_string(b);
B+=
to_string(a);
return A<
B;
}
string PrintMinNumber(vector<
int>
numbers)
{
string res;
sort(numbers.begin(), numbers.end(), cmp);
for(
int i =
0; i < numbers.size(); i++
)
{
res+=
to_string(numbers[i]);
}
return res;
}
};
转载于:https://www.cnblogs.com/ruoh3kou/p/10125188.html