diff --git a/2201/220109.cpp b/2201/220109.cpp index 8b0556f..6ae4856 100644 --- a/2201/220109.cpp +++ b/2201/220109.cpp @@ -1,5 +1,6 @@ #include #include +#include /** * 1041. Robot Bounded In Circle diff --git a/2201/220110-CN.cpp b/2201/220110-CN.cpp new file mode 100644 index 0000000..b0f45cf --- /dev/null +++ b/2201/220110-CN.cpp @@ -0,0 +1,60 @@ +#include + +class Solution { +private: + inline static unsigned long long readStr(const char* const str, int L, int R) { + unsigned long long ret = 0; + for (int i = L; str[i] && i < R; ++i) + ret = (ret << 3) + (ret << 1) + (str[i] ^ 48); + return ret; + } +public: + static bool isAdditiveNumber(const std::string& num) { + int len = num.length(); + const char* const str = num.c_str(); + for (int i = 1, stop = 0; !stop && i + i <= len; ++i) { + // i: length of 1st number + // printf("+ 1st num has length %d\n", i); + for (int j = 1, stopJ = 0; !stopJ && i + j + j <= len; ++j) { + // j: length of 2nd number + // printf("++ 2nd num has length %d\n", j); + unsigned long long n1 = readStr(str, 0, i); + unsigned long long n2 = readStr(str, i, i + j); + for (int pos = i + j; pos < len; ) { + unsigned long long n3 = n1 + n2; + int n3L = 0; + for (unsigned long long tmp = n3; tmp; tmp /= 10, ++n3L); + if (n3 == 0) n3L = 1; + // printf("+++ next num should be %lld, has length %d, should at %d\n", n3, n3L, pos); + if (pos + n3L > len) { + // printf("+++ next num length exceeded, abort j.\n"); + stopJ = 1; + break; + } + unsigned long long readNum = readStr(str, pos, pos + n3L); + // printf("+++ next num is %lld.\n", readNum); + if (readNum == n3) { + // printf("+++ next num is validated.\n"); + if (pos + n3L == len) + return true; + pos = pos + n3L; + n1 = n2; + n2 = n3; + // printf("+++ next position is %d.\n", pos); + } else { + // printf("+++ next num is not validated, try next j.\n"); + break; + } + } + if (*(str + i) == '0') break; + } + if (*str == '0') break; + } + return false; + } +}; + +int main() { + // printf("%s\n", Solution::isAdditiveNumber("123") ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index c2299eb..278f84f 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220109.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220110-CN.cpp) \ No newline at end of file