diff --git a/cpp/2205/220503-CN.cpp b/cpp/2205/220503-CN.cpp new file mode 100644 index 0000000..f9f0b19 --- /dev/null +++ b/cpp/2205/220503-CN.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +/** + * 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 reorderLogFiles(std::vector& logs) { + std::vector 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; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 8309278..bcb81a9 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220502-CN.cpp) +ADD_EXECUTABLE(2204 220503-CN.cpp)