From f96d8bc6cbbd98b86afdacd549692ca6f0fd5ad3 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 1 Mar 2022 23:45:37 +0800 Subject: [PATCH] add: 220301 [cpp] --- cpp/2203/220301.cpp | 24 ++++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220301.cpp diff --git a/cpp/2203/220301.cpp b/cpp/2203/220301.cpp new file mode 100644 index 0000000..d70df49 --- /dev/null +++ b/cpp/2203/220301.cpp @@ -0,0 +1,24 @@ +#include +#include + +/** + * 338. Counting Bits + * Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. + */ + +class Solution { +public: + static std::vector countBits(int n) { + std::vector ans(n + 1); + for (int i = 0; i <= n; ++i) + ans[i] = __builtin_popcount(i); + return ans; + } +}; + +int main() { + auto ret = Solution::countBits(5); + for (int i : ret) + std::printf("%d ", i); + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index a5667fb..17c58fa 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 220301-CN.cpp) +ADD_EXECUTABLE(2203 220301.cpp)