-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest Number.cpp
More file actions
35 lines (27 loc) · 820 Bytes
/
Largest Number.cpp
File metadata and controls
35 lines (27 loc) · 820 Bytes
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
class Solution {
private:
static bool cmp(const string& str1, const string& str2) {
return str1+str2 > str2+str1;
}
public:
string largestNumber(vector<int> &num) {
vector<string> keep;
int zerocount = 0;
if (num.size() == 0)
return NULL;
for (int n = 0; n < num.size(); n++) {
if(num[n] == 0)
zerocount++;
keep.push_back(to_string(num[n]));
}
if(num.size() == zerocount)
return "0";
if(num.size() == 1)
return keep[0];
string result;
sort(keep.begin(), keep.end(), cmp);
for(int n = 0; n < keep.size(); n++)
result += keep[n];
return result;
}
};