From 4a8dbeffa5e40d9a1b2e6af10d990882fb59be88 Mon Sep 17 00:00:00 2001 From: Yaha Date: Wed, 17 May 2023 22:54:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ CMakeLists.txt | 37 ++++++++++++++++++++++++++++++ main.cpp | 6 +++++ src/map/two_sum.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 src/map/two_sum.cpp diff --git a/.gitignore b/.gitignore index e257658..2f2ce88 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ *.out *.app +.idea/ +cmake-build-debug/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3dddb25 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.25) +project(leetcode) + +set(CMAKE_CXX_STANDARD 17) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) +include(GoogleTest) + +set(SOURCES) +file(GLOB_RECURSE SOURCES "src/*/*.cpp") + +enable_testing() + +# Iterate over each source file +message(STATUS "========= enable_testing ========") +foreach(SOURCE ${SOURCES}) + # Get the file name without extension + get_filename_component(APP_NAME ${SOURCE} NAME_WE) + + message(STATUS "==== app_name: ${APP_NAME} path: ${SOURCE}") + # Add an executable target for each file + add_executable(${APP_NAME} ${SOURCE}) + target_link_libraries( + ${APP_NAME} + GTest::gtest_main + ) + + gtest_discover_tests(${APP_NAME}) +endforeach() +message(STATUS "========= end testing ========") diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0ab7f9b --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/src/map/two_sum.cpp b/src/map/two_sum.cpp new file mode 100644 index 0000000..79e4052 --- /dev/null +++ b/src/map/two_sum.cpp @@ -0,0 +1,55 @@ +// +// Created by Yaha on 2023/5/17. +// +#include +#include + +#include + +/* + 示例 1: + + 输入:nums = [2,7,11,15], target = 9 + 输出:[0,1] + 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 + 示例 2: + + 输入:nums = [3,2,4], target = 6 + 输出:[1,2] + 示例 3: + + 输入:nums = [3,3], target = 6 + 输出:[0,1] +   + + 提示: + + 2 <= nums.length <= 104 + -109 <= nums[i] <= 109 + -109 <= target <= 109 + 只会存在一个有效答案 + + 来源:力扣(LeetCode) + 链接:https://leetcode.cn/problems/two-sum + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +/** + * @param nums + * @param target + * @return + */ +std::vector twoSum(std::vector& nums, int target) { + std::map m; + for (int i = 0; i < nums.size(); ++i) { + if (target - nums[i] == 0) { + + } + } + +} + +TEST(TwoSumTest, BasicAssertions) { + + auto a = twoSum(std::vector{2,7,11,15}, 9); +} \ No newline at end of file