-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
43 lines (42 loc) · 991 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
bool cmp(const string s1, const string s2)
{
return (s1 + s2) > (s2 + s1);
}
class Solution
{
public:
string largestNumber(vector<int> &num)
{
vector<string> s_num(num.size());
stringstream stream;
for (int i = 0; i < num.size(); ++i)
{
stream << num[i];
stream >> s_num[i];
stream.clear();
}
sort(s_num.begin(), s_num.end(), cmp);
string tmp_res;
for (int i = 0; i < s_num.size(); ++i)
{
tmp_res += s_num[i];
}
string res;
bool flag = false;
for (int i = 0; i < tmp_res.size(); ++i)
{
if (tmp_res[i] != '0')
{
res.push_back(tmp_res[i]);
flag = true;
}
else if (flag)
{
res.push_back(tmp_res[i]);
}
}
if (!flag)
res.push_back('0');
return res;
}
};