diff --git a/include/solution/338.h b/include/solution/338.h new file mode 100644 index 0000000..6f66664 --- /dev/null +++ b/include/solution/338.h @@ -0,0 +1,28 @@ +// +// Created by aurora on 2024/9/16. +// + +#ifndef LEETCODE_338_H +#define LEETCODE_338_H +// 给你一个整数 n ,对于 0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans +// 作为答案。 示例 1: 输入:n = 2 输出:[0,1,1] 解释: 0 --> 0 1 --> 1 2 --> 10 +// +// 示例 2: +// 输入:n = 5 +// 输出:[0,1,1,2,1,2] +// 解释: +// 0 --> 0 +// 1 --> 1 +// 2 --> 10 +// 3 --> 11 +// 4 --> 100 +// 5 --> 101 +// +// 提示: +// 0 <= n <= 105 +// +// 进阶: +// 很容易就能实现时间复杂度为 O(n log n) 的解决方案,你可以在线性时间复杂度 O(n) 内用一趟扫描解决此问题吗? +// 你能不使用任何内置函数解决此问题吗?(如,C++ 中的 __builtin_popcount ) +int *countBits(int n, int *returnSize); +#endif // LEETCODE_338_H diff --git a/src/338.c b/src/338.c new file mode 100644 index 0000000..17fb4b1 --- /dev/null +++ b/src/338.c @@ -0,0 +1,7 @@ +// +// Created by aurora on 2024/9/16. +// +#include +int *countBits(int n, int *returnSize) +{ +} \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..1eaf795 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB SRC_FILES "*.cpp") + +foreach(SRC_FILE ${SRC_FILES}) + get_filename_component(EXE_NAME ${SRC_FILE} NAME_WE) + add_executable(${EXE_NAME} ${SRC_FILE}) + target_link_libraries(${EXE_NAME} gtest gmock) +endforeach() \ No newline at end of file diff --git a/tests/test_338.cpp b/tests/test_338.cpp new file mode 100644 index 0000000..01d9b63 --- /dev/null +++ b/tests/test_338.cpp @@ -0,0 +1,12 @@ +// +// Created by aurora on 2024/9/16. +// +#include +#include + +TEST(CountingBits, 1) +{ + int size = 0; + int ans[] = {0, 1, 1}; + EXPECT_EQ(ans, countBits(2, &size)); +} \ No newline at end of file