diff --git a/cpp/2203/220331-CN.cpp b/cpp/2203/220331-CN.cpp new file mode 100644 index 0000000..fb57adc --- /dev/null +++ b/cpp/2203/220331-CN.cpp @@ -0,0 +1,39 @@ +#include + +template +class AnswerTable { +public: + bool isNotAnswer[N]{}; + + explicit constexpr AnswerTable() { + for (int i = 1; i < N; ++i) { + for (int j = i; j; j /= 10) { + if (!(j % 10) || i % (j % 10)) { + isNotAnswer[i] = true; + break; + } + } + } + } +}; + +/** + * 728. Self Dividing Numbers + * A self-dividing number is a number that is divisible by every digit it contains. + * - For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. + * A self-dividing number is not allowed to contain the digit zero. + * Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right]. + */ + +class Solution { +private: + static constexpr AnswerTable<10001> ans = AnswerTable<10001>(); +public: + static std::vector selfDividingNumbers(int left, int right) { + std::vector ret; + for (int i = left; i <= right; ++i) + if (!ans.isNotAnswer[i]) + ret.push_back(i); + return ret; + } +}; diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 0e6e95b..fa4be17 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220330-CN.cpp) +ADD_EXECUTABLE(2203 220331-CN.cpp)