diff --git a/cpp/2204/220411-CN.cpp b/cpp/2204/220411-CN.cpp new file mode 100644 index 0000000..3293f62 --- /dev/null +++ b/cpp/2204/220411-CN.cpp @@ -0,0 +1,35 @@ +#include + +/** + * 357. Count Numbers with Unique Digits + * Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10^n. + */ + +class Solution { +public: + static int countNumbersWithUniqueDigits(int n) { + static const int ans[] = {1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851}; + return ans[n]; + } +}; + +bool isUnique(int x) { + int c[10]{}; + for (; x; x /= 10) + if (c[x % 10]++) + return false; + return true; +} + +int main() { + int ans = 0; + for (int i = 0, j = 1; i < 9; ++i, j *= 10) { + for (int k = j / 10; k < j; ++k) { + if (isUnique(k)) { + ++ans; + } + } + std::printf("%d(%d) -> %d\n", i, j, ans); + } + return 0; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 9de3d26..2e12fe4 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220410-CN.cpp) +ADD_EXECUTABLE(2204 220411-CN.cpp)