This commit is contained in:
Sainnhe Park 2022-03-08 15:54:17 +08:00
parent 57d75f7596
commit 2af9a81f28
5 changed files with 131 additions and 8 deletions

29
.gitignore vendored
View File

@ -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/

38
CMakeLists.txt Normal file
View File

@ -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
)

27
include/s1_two_sum.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef S1_TWO_SUM_HPP
#define S1_TWO_SUM_HPP
#include <iostream>
#include <unordered_map>
#include <vector>
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<int> twoSum(std::vector<int>& nums, int target);
};
#endif

14
src/s1_two_sum.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "s1_two_sum.hpp"
using namespace std;
vector<int> Solution::twoSum(vector<int>& nums, int target) {
unordered_map<int, int> 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 {};
}

31
tests/s1_two_sum.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "s1_two_sum.hpp"
#include <gtest/gtest.h>
#include <vector>
TEST(Problem1, Case1) {
std::vector<int> input1{ 2, 7, 11, 15 };
int input2{ 9 };
Solution solution;
std::vector<int> result = solution.twoSum(input1, input2);
std::vector<int> answer = std::vector<int>{ 0, 1 };
EXPECT_EQ(result, answer);
}
TEST(Problem1, Case2) {
std::vector<int> input1{ 3, 2, 4 };
int input2{ 6 };
Solution solution;
std::vector<int> result = solution.twoSum(input1, input2);
std::vector<int> answer = std::vector<int>{ 1, 2 };
EXPECT_EQ(result, answer);
}
TEST(Problem1, Case3) {
std::vector<int> input1{ 3, 3 };
int input2{ 6 };
Solution solution;
std::vector<int> result = solution.twoSum(input1, input2);
std::vector<int> answer = std::vector<int>{ 0, 1 };
EXPECT_EQ(result, answer);
}