add: 220503-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-03 14:26:27 +08:00
parent fc61c3d184
commit 1267f7e304
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 42 additions and 1 deletions

41
cpp/2205/220503-CN.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
/**
* 937. Reorder Data in Log Files
* 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.
*/
class Solution {
public:
static std::vector<std::string> reorderLogFiles(std::vector<std::string>& logs) {
std::vector<std::string> l, d;
auto getContent = [](const std::string& str) {
return str.substr(str.find(' ') + 1);
};
for (auto s : logs) {
(std::any_of(s.begin() + s.find(' '), s.end(), [](char c){ return std::isalpha(c); }) ? &l : &d)->emplace_back(std::move(s));
}
std::sort(l.begin(), l.end(), [&](const std::string& x, const std::string& y){
auto cx = getContent(x), cy = getContent(y);
return cx == cy ? x < y : cx < cy;
});
l.insert(l.end(), d.begin(), d.end());
return l;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220502-CN.cpp)
ADD_EXECUTABLE(2204 220503-CN.cpp)