Two Sum
This commit is contained in:
parent
57d75f7596
commit
2af9a81f28
29
.gitignore
vendored
29
.gitignore
vendored
@ -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))
|
# create by https://github.com/iamcco/coc-gitignore (Tue Mar 08 2022 16:17:40 GMT+0800 (China Standard Time))
|
||||||
# Rust.gitignore:
|
# CMake.gitignore:
|
||||||
# Generated by Cargo
|
CMakeLists.txt.user
|
||||||
# will have compiled files and executables
|
CMakeCache.txt
|
||||||
/target/
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# CMake.patch:
|
||||||
**/*.rs.bk
|
# External projects
|
||||||
|
*-prefix/
|
||||||
|
38
CMakeLists.txt
Normal file
38
CMakeLists.txt
Normal 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
27
include/s1_two_sum.hpp
Normal 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
14
src/s1_two_sum.cpp
Normal 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
31
tests/s1_two_sum.cpp
Normal 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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user