leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit fbd9ae81da0f386c97bd88787e1e0cb6d1cfde05
parent 1b582cc3beb276d22d7dd0775908eaa1c4e74a23
Author: AndrewLockVI <andrew.laack@gmail.com>
Date:   Wed,  5 Apr 2023 17:21:28 -0500

Added new solution using hashmap

Diffstat:
Atwo-sums/two-sums-V3-hashtable.cpp | 42++++++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+), 0 deletions(-)

diff --git a/two-sums/two-sums-V3-hashtable.cpp b/two-sums/two-sums-V3-hashtable.cpp @@ -0,0 +1,42 @@ +/*Conceptual: + + 2 9 8 7 8 0 9 1 8 6 78 72 88 19 62 + _ _ _ _ _ _ _ _ _ _ __ __ __ __ __ +[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] + + + + + + +*/ + + +#include <iostream> +#include <vector> +#include <map> + +using namespace std; + +vector<int> twoSum(vector<int>& nums, int target) { + map<int, int> vals_map; + int second_val; + int first_val; + for(int i = 0 ; i < nums.size(); ++i){ + int diff = target - nums[i]; + if(vals_map.count(diff)){ + second_val = vals_map.find(diff)->second; + first_val = i; + break; + } + vals_map.insert( pair<int, int> (nums[i] , i)); + } + return {first_val, second_val}; +} + +int main(){ + vector<int> input = {0 , 3, 1}; + vector<int> new_vec = twoSum(input , 4); + cout << new_vec[0] << " " << new_vec[1] << endl; + return 0; +}