diff --git a/.gitignore b/.gitignore index 2f2ce88..11d1f09 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ *.app .idea/ -cmake-build-debug/ \ No newline at end of file +cmake-build-debug/ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dddb25..17b8012 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,36 +2,12 @@ cmake_minimum_required(VERSION 3.25) project(leetcode) set(CMAKE_CXX_STANDARD 17) +set(APP_LIB_NAME leetcode) -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) +include_directories(include) -set(SOURCES) file(GLOB_RECURSE SOURCES "src/*/*.cpp") +add_library(${APP_LIB_NAME} ${SOURCES}) -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 ========") +include(cmake/googletest.cmake) +include(tests/tests.cmake) \ No newline at end of file diff --git a/cmake/googletest.cmake b/cmake/googletest.cmake new file mode 100644 index 0000000..acfbff9 --- /dev/null +++ b/cmake/googletest.cmake @@ -0,0 +1,9 @@ +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) \ No newline at end of file diff --git a/include/map_vec.h b/include/map_vec.h new file mode 100644 index 0000000..f01c2e3 --- /dev/null +++ b/include/map_vec.h @@ -0,0 +1,6 @@ +#pragma once +#include + +namespace yaha { +std::vector two_sum(std::vector &nums, int target); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 0ab7f9b..0000000 --- a/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#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 index 79e4052..1f5d623 100644 --- a/src/map/two_sum.cpp +++ b/src/map/two_sum.cpp @@ -1,55 +1,22 @@ -// -// 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 - 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 - */ - +#include /** * @param nums * @param target * @return */ -std::vector twoSum(std::vector& nums, int target) { - std::map m; +namespace yaha { +std::vector two_sum(std::vector &nums, int target) { + std::map result_map; + std::vector result; for (int i = 0; i < nums.size(); ++i) { - if (target - nums[i] == 0) { - + auto it = result_map.find(target - nums[i]); + if (it != result_map.end()) { + return {it->second, i}; } + result_map[nums[i]] = i; } + return {}; } -TEST(TwoSumTest, BasicAssertions) { - - auto a = twoSum(std::vector{2,7,11,15}, 9); -} \ No newline at end of file +} diff --git a/tests/test_main.cpp b/tests/test_main.cpp new file mode 100644 index 0000000..1da8654 --- /dev/null +++ b/tests/test_main.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char* argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/tests/tests.cmake b/tests/tests.cmake new file mode 100644 index 0000000..bbce010 --- /dev/null +++ b/tests/tests.cmake @@ -0,0 +1,22 @@ +set(APP_TEST_NAME leetcode_test) + +message(STATUS =========== start testing APP_TEST_NAME: ${APP_TEST_NAME} ==========) +set(TEST_SOURCES) +file(GLOB_RECURSE TEST_SOURCES "tests/*.cpp") +message(STATUS =========== test_file: ${TEST_SOURCES} ==========) + +enable_testing() +add_executable( + ${APP_TEST_NAME} + tests/test_main.cpp + ${TEST_SOURCES} +) +target_link_libraries( + ${APP_TEST_NAME} + GTest::gtest_main + ${APP_LIB_NAME} +) + +include(GoogleTest) +gtest_discover_tests(${APP_TEST_NAME}) +message(STATUS "========= end testing ========") diff --git a/tests/two_sum_test.cpp b/tests/two_sum_test.cpp new file mode 100644 index 0000000..90c46f0 --- /dev/null +++ b/tests/two_sum_test.cpp @@ -0,0 +1,62 @@ +#include + +#include "map_vec.h" + +/** + * 2 <= nums.length <= 104 + * -109 <= nums[i] <= 109 + * -109 <= target <= 109 + * 只会存在一个有效答案 + * + * 来源:力扣(LeetCode) + * 链接:https://leetcode.cn/problems/two-sum + * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ +TEST(TwoSumTest, BasicAssertions) { +std::vector output, input; +int target; + +/** + * 示例 1: + * 输入:nums = [2,7,11,15], target = 9 + * 输出:[0,1] + * 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 + */ +input.resize(4); +input = {2, 7, 11, 15}; +target = 9; + +output = yaha::two_sum(input, target); +ASSERT_EQ(output.size(), 2); +ASSERT_EQ(output[0], 0); +ASSERT_EQ(output[1], 1); + +/** + * 示例 2: + * 输入:nums = [3,2,4], target = 6 + * 输出:[1,2] + */ +input.resize(3); +input = {3, 2, 4}; +target = 6; + +output = yaha::two_sum(input, target); +ASSERT_EQ(output.size(), 2); +ASSERT_EQ(output[0], 1); +ASSERT_EQ(output[1], 2); + +/** + * 示例 3: + * 输入:nums = [3,3], target = 6 + * 输出:[0,1] + */ +input.resize(2); +input = {3, 3}; +target = 6; + +output = yaha::two_sum(input, target); +ASSERT_EQ(output.size(), 2); +ASSERT_EQ(output[0], 0); +ASSERT_EQ(output[1], 1); + +} \ No newline at end of file