diff --git a/cpp/2308/LC230927.cpp b/cpp/2308/LC230927.cpp new file mode 100644 index 0000000..00a34bd --- /dev/null +++ b/cpp/2308/LC230927.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +#include "defs.h" + +std::string LC230927::decodeAtIndex(const std::string& s, int k) { + std::vector> col; + unsigned long long len = 0; + std::string buf; + for (char ch : s) { + if (std::isalpha(ch)) { + ++len; + buf += ch; + } else { // num + col.emplace_back(buf, ch ^ 0x30, len *= (ch ^ 0x30)); + buf.clear(); + } + if (len >= k) + break; + } + if (!buf.empty()) + col.emplace_back(buf, 1, len); + const int level = col.size(); + int ck = k - 1; + for (int i = level - 1; i >= 0; --i) { + const auto prev_len = i ? std::get<2>(col[i - 1]) : 0ULL; + const auto& [c_str, c_dup, cur_len] = col[i]; + ck %= (cur_len / c_dup); + if (ck >= prev_len) + return {c_str[ck - prev_len]}; + } + return {std::get<0>(col[0])[ck]}; +} + + diff --git a/cpp/2308/defs.h b/cpp/2308/defs.h index e8db557..42b50a7 100644 --- a/cpp/2308/defs.h +++ b/cpp/2308/defs.h @@ -26,4 +26,9 @@ public: static int bestClosingTime(const std::string&); }; +class LC230927 { +public: + static std::string decodeAtIndex(const std::string& s, int k); +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2308/main.cpp b/cpp/2308/main.cpp index 277c8cd..8be3ac7 100644 --- a/cpp/2308/main.cpp +++ b/cpp/2308/main.cpp @@ -2,7 +2,6 @@ #include int main() { - LC230827 s; - std::cout << s.canCross({0,1,3,5,6,8,12,17}); + std::cout << LC230927::decodeAtIndex("a2b3c4d5e6f7g8h9", 9); return 0; } \ No newline at end of file