diff --git a/cpp/2210/221001.cpp b/cpp/2210/221001.cpp new file mode 100644 index 0000000..55d5479 --- /dev/null +++ b/cpp/2210/221001.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +/** + * 91. Decode Ways + * + * A message containing letters from A-Z can be encoded into numbers using the following mapping: + * + * 'A' -> "1" + * 'B' -> "2" + * ... + * 'Z' -> "26" + * To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into: + * + * "AAJF" with the grouping (1 1 10 6) + * "KJF" with the grouping (11 10 6) + * Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06". + * + * Given a string s containing only digits, return the number of ways to decode it. + * + * The test cases are generated so that the answer fits in a 32-bit integer. + */ + +class Solution { +private: + int ans[105]{}; + + int help(const char* const t, int pos = 0) { + if (ans[pos] != -1) + return ans[pos]; + const char* const s = t + pos; + if (!*s) + return 1; + if ('0' == *s) + return 0; + int ret = help(t, pos + 1); + if (10 * (s[0] ^ 48) + (s[1] ^ 48) <= 26) + ret += help(t, pos + 2); + return ans[pos] = ret; + } +public: + int numDecodings(const std::string& s) { + std::memset(ans, -1, sizeof ans); + return help(s.c_str()); + } +}; + +int main() { + Solution s; + std::cout << s.numDecodings("111111111111111111111111111111111111111111111"); + return 0; +} diff --git a/cpp/2210/CMakeLists.txt b/cpp/2210/CMakeLists.txt new file mode 100644 index 0000000..b10edb7 --- /dev/null +++ b/cpp/2210/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.23) +PROJECT(2210) + +SET(CMAKE_CXX_STANDARD 23) + +ADD_EXECUTABLE(2210 221001.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index c10d537..f3e632d 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -25,5 +25,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp) # ADD_SUBDIRECTORY(2204) # ADD_SUBDIRECTORY(2205) # ADD_SUBDIRECTORY(2206) -ADD_SUBDIRECTORY(2207) +# ADD_SUBDIRECTORY(2207) +ADD_SUBDIRECTORY(2210) ADD_SUBDIRECTORY(more)