-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path937.cpp
More file actions
39 lines (37 loc) · 1.19 KB
/
937.cpp
File metadata and controls
39 lines (37 loc) · 1.19 KB
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
class Solution {
public:
static vector<string> log2vector(const string& log) {
stringstream ss(log);
string token;
vector<string> out;
while (getline(ss, token, ' ')) out.push_back(token);
return out;
}
static bool compare(const string& log1, const string& log2) {
vector<string> log1v = log2vector(log1);
vector<string> log2v = log2vector(log2);
int m = log1v.size();
int n = log2v.size();
int k = min(m, n);
for (int i = 1; i < k; i++) {
if (log1v[i] != log2v[i]) return log1v[i] < log2v[i];
}
if (m != n) return m < n;
return log1v[0] < log2v[0];
}
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> letters;
vector<string> digits;
for (auto log : logs) {
int index = log.find(' ');
char c = log[index + 1];
if (c >= '0' && c <= '9') digits.push_back(log);
else letters.push_back(log);
}
sort(letters.begin(), letters.end(), compare);
for (auto log : digits) {
letters.push_back(log);
}
return letters;
}
};