-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
30 lines (25 loc) · 938 Bytes
/
main.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
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
unordered_map<string, string> log2key;
unordered_map<string, string> log2id;
for (int i = 0; i < logs.size(); ++i) {
const string& s = logs[i];
stringstream ss;
if (isdigit(s.back())) {
ss << "1" << setw(3) << setfill('0') << i;
} else { // isdigit(s.back()) == false
int l = find(s.begin(), s.end(), ' ') - s.begin() + 1;
ss << "0" << s.substr(l);
log2id[s] = s.substr(0, l);
}
log2key[s] = ss.str();
}
sort(logs.begin(), logs.end(),
[&](const string& a, const string& b) -> bool {
if (log2key[a] != log2key[b]) return log2key[a] < log2key[b];
return log2id[a] < log2id[b];
});
return logs;
}
};