Compare commits
3 Commits
1e9de7c9c2
...
2cdaeb0e65
| Author | SHA1 | Date | |
|---|---|---|---|
| 2cdaeb0e65 | |||
| 5ffc8c8eb0 | |||
| b1893c001e |
@@ -1,11 +1,13 @@
|
|||||||
cmake_minimum_required(VERSION 3.30)
|
cmake_minimum_required(VERSION 3.30)
|
||||||
project(leetcode C)
|
project(leetcode C CXX)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
add_compile_options(
|
add_compile_options(
|
||||||
-Wall
|
-Wall
|
||||||
-Wextra
|
-Wextra
|
||||||
@@ -21,6 +23,14 @@ add_link_options(
|
|||||||
-fsanitize=undefined
|
-fsanitize=undefined
|
||||||
)
|
)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://github.com/google/googletest/releases/download/v1.15.2/googletest-1.15.2.tar.gz
|
||||||
|
URL_HASH MD5=7e11f6cfcf6498324ac82d567dcb891e
|
||||||
|
)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
file(GLOB SRC_FILES "src/*.c")
|
file(GLOB SRC_FILES "src/*.c")
|
||||||
|
|
||||||
foreach (SRC_FILE ${SRC_FILES})
|
foreach (SRC_FILE ${SRC_FILES})
|
||||||
@@ -28,3 +38,4 @@ foreach(SRC_FILE ${SRC_FILES})
|
|||||||
add_executable(${EXE_NAME} ${SRC_FILE})
|
add_executable(${EXE_NAME} ${SRC_FILE})
|
||||||
endforeach ()
|
endforeach ()
|
||||||
|
|
||||||
|
add_subdirectory(tests)
|
||||||
|
|||||||
28
include/solution/338.h
Normal file
28
include/solution/338.h
Normal file
@@ -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
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
|
|
||||||
int max(const int a, const int b)
|
int max(const int a, const int b)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
|
struct TreeNode *sortedArrayToBST(int *nums, int numsSize)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
int minDepth(struct TreeNode *root)
|
int minDepth(struct TreeNode *root)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
bool in_order(struct TreeNode *root, int target, int sum)
|
bool in_order(struct TreeNode *root, int target, int sum)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/ListNode.h"
|
#include "ListNode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
bool hasCycle(struct ListNode *head)
|
bool hasCycle(struct ListNode *head)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct list_t
|
struct list_t
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "include/TreeNode.h"
|
#include "TreeNode.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct list_t
|
struct list_t
|
||||||
|
|||||||
7
src/338.c
Normal file
7
src/338.c
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
//
|
||||||
|
// Created by aurora on 2024/9/16.
|
||||||
|
//
|
||||||
|
#include <solution/338.h>
|
||||||
|
int *countBits(int n, int *returnSize)
|
||||||
|
{
|
||||||
|
}
|
||||||
7
tests/CMakeLists.txt
Normal file
7
tests/CMakeLists.txt
Normal file
@@ -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()
|
||||||
12
tests/test_338.cpp
Normal file
12
tests/test_338.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//
|
||||||
|
// Created by aurora on 2024/9/16.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/338.h>
|
||||||
|
|
||||||
|
TEST(CountingBits, 1)
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
int ans[] = {0, 1, 1};
|
||||||
|
EXPECT_EQ(ans, countBits(2, &size));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user