From 2af9a81f28b567a40bf144481f67c7a30a2e6419 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Tue, 8 Mar 2022 15:54:17 +0800 Subject: [PATCH] Two Sum --- .gitignore | 29 +++++++++++++++++++++-------- CMakeLists.txt | 38 ++++++++++++++++++++++++++++++++++++++ include/s1_two_sum.hpp | 27 +++++++++++++++++++++++++++ src/s1_two_sum.cpp | 14 ++++++++++++++ tests/s1_two_sum.cpp | 31 +++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 include/s1_two_sum.hpp create mode 100644 src/s1_two_sum.cpp create mode 100644 tests/s1_two_sum.cpp diff --git a/.gitignore b/.gitignore index 815e02a..a887e69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,23 @@ -target +.cache +.cmake +bin +lib +leetcode* -# create by https://github.com/iamcco/coc-gitignore (Sun Jan 02 2022 18:30:21 GMT+0800 (China Standard Time)) -# Rust.gitignore: -# Generated by Cargo -# will have compiled files and executables -/target/ +# create by https://github.com/iamcco/coc-gitignore (Tue Mar 08 2022 16:17:40 GMT+0800 (China Standard Time)) +# CMake.gitignore: +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps -# These are backup files generated by rustfmt -**/*.rs.bk +# CMake.patch: +# External projects +*-prefix/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..878b83b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.14) +project(leetcode) + +# GoogleTest requires at least C++11 +set(CMAKE_CXX_STANDARD 11) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +FILE(GLOB Sources src/*.cpp) +FILE(GLOB Tests tests/*.cpp) + +add_executable( + leetcode + ${Sources} + ${Tests} +) +target_link_libraries( + leetcode + gtest_main +) + +include(GoogleTest) +gtest_discover_tests(leetcode) + +target_include_directories(leetcode + PRIVATE + include +) + diff --git a/include/s1_two_sum.hpp b/include/s1_two_sum.hpp new file mode 100644 index 0000000..402dbe4 --- /dev/null +++ b/include/s1_two_sum.hpp @@ -0,0 +1,27 @@ +#ifndef S1_TWO_SUM_HPP +#define S1_TWO_SUM_HPP + +#include +#include +#include + +class Solution { + public: + /** + * @brief Two Sum + * + * Given an array of integers `nums` and an integer `target`, return indices + * of the two numbers such that they add up to `target`. + * + * You may assume that each input would have **exactly one solution**, and you + * may not use the same element twice. + * + * You can return the answer in any order. + * + * @param nums the array of integers to be summed + * @param target the target integer + */ + std::vector twoSum(std::vector& nums, int target); +}; + +#endif diff --git a/src/s1_two_sum.cpp b/src/s1_two_sum.cpp new file mode 100644 index 0000000..e043dd6 --- /dev/null +++ b/src/s1_two_sum.cpp @@ -0,0 +1,14 @@ +#include "s1_two_sum.hpp" +using namespace std; + +vector Solution::twoSum(vector& nums, int target) { + unordered_map hashtable; + for (int i = 0; i < nums.size(); ++i) { + auto it = hashtable.find(target - nums[i]); + if (it != hashtable.end()) { + return {it->second, i}; + } + hashtable[nums[i]] = i; + } + return {}; +} diff --git a/tests/s1_two_sum.cpp b/tests/s1_two_sum.cpp new file mode 100644 index 0000000..cb3cdf9 --- /dev/null +++ b/tests/s1_two_sum.cpp @@ -0,0 +1,31 @@ +#include "s1_two_sum.hpp" + +#include +#include + +TEST(Problem1, Case1) { + std::vector input1{ 2, 7, 11, 15 }; + int input2{ 9 }; + Solution solution; + std::vector result = solution.twoSum(input1, input2); + std::vector answer = std::vector{ 0, 1 }; + EXPECT_EQ(result, answer); +} + +TEST(Problem1, Case2) { + std::vector input1{ 3, 2, 4 }; + int input2{ 6 }; + Solution solution; + std::vector result = solution.twoSum(input1, input2); + std::vector answer = std::vector{ 1, 2 }; + EXPECT_EQ(result, answer); +} + +TEST(Problem1, Case3) { + std::vector input1{ 3, 3 }; + int input2{ 6 }; + Solution solution; + std::vector result = solution.twoSum(input1, input2); + std::vector answer = std::vector{ 0, 1 }; + EXPECT_EQ(result, answer); +}