The description of problem
You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
There are two types of logs:
Letter-logs: All words (except the identifier) consist of lowercase English letters.
Digit-logs: All words (except the identifier) consist of digits.
Reorder these logs so that:
The letter-logs come before all digit-logs.
The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
The digit-logs maintain their relative ordering.
Return the final order of the logs.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files
example
Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-data-in-log-files
The intuition for this problem
- pick out the digit logs and character logs into different vectors
- sort out the character logs
The codes for this
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> digit_logs;
vector<string> character_logs;
for (auto log : logs) {
if (is_digit_log(log)) {
digit_logs.push_back(log);
} else {
character_logs.push_back(log);
}
}
// sort out the character_logs
sort(character_logs.begin(), character_logs.end(), [](string& a, string& b) {
string a_key = a.substr(a.find(' ') + 1);
string b_key = b.substr(b.find(' ') + 1);
if (a_key == b_key) {
return a < b;
} else {
return a_key < b_key;
}
});
// merge the two vectors
vector<string> result;
result.reserve(logs.size());
result.insert(result.end(), character_logs.begin(), character_logs.end());
result.insert(result.end(), digit_logs.begin(), digit_logs.end());
return result;
}
bool is_digit_log(string log) {
// check if the char after the first space is a digit
return log[log.find(' ') + 1] >= '0' && log[log.find(' ') + 1] <= '9';
}
};
int main()
{
vector<string> logs = {"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};
Solution _s;
vector<string> result = _s.reorderLogFiles(logs);
for (auto log : result) {
cout << log << endl;
}
}
The corresponding results
let1 art can
let3 art zero
let2 own kit dig
dig1 8 1 5 1
dig2 3 6
The little improvement for above codes
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> digits_logs;
vector<string> letters_logs;
for (auto log : logs) {
if (isdigit(log[log.find(' ') + 1])) {
digits_logs.push_back(log);
} else {
letters_logs.push_back(log);
}
}
sort(letters_logs.begin(), letters_logs.end(), [](string& a, string& b) {
string a_key = a.substr(a.find(' ') + 1);
string b_key = b.substr(b.find(' ') + 1);
if (a_key == b_key) {
return a < b;
} else {
return a_key < b_key;
}
});
letters_logs.insert(letters_logs.end(), digits_logs.begin(), digits_logs.end());
return letters_logs;
}
};
int main()
{
vector<string> logs = {"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"};
Solution _s;
vector<string> result = _s.reorderLogFiles(logs);
for (auto log : result) {
cout << log << endl;
}
}