Skip to content

Commit

Permalink
Added solution for 1. Two Sum: https://leetcode.com/problems/two-sum/
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Oct 18, 2024
1 parent 6c15f38 commit b6a69b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "413"
endingLineNumber = "413"
startingLineNumber = "415"
endingLineNumber = "415"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "379"
endingLineNumber = "379"
startingLineNumber = "381"
endingLineNumber = "381"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "304"
endingLineNumber = "304"
startingLineNumber = "306"
endingLineNumber = "306"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "272"
endingLineNumber = "272"
startingLineNumber = "274"
endingLineNumber = "274"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "232"
endingLineNumber = "232"
startingLineNumber = "234"
endingLineNumber = "234"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "195"
endingLineNumber = "195"
startingLineNumber = "197"
endingLineNumber = "197"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -158,8 +158,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "154"
endingLineNumber = "154"
startingLineNumber = "156"
endingLineNumber = "156"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "143"
endingLineNumber = "143"
startingLineNumber = "145"
endingLineNumber = "145"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -206,8 +206,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "129"
endingLineNumber = "129"
startingLineNumber = "131"
endingLineNumber = "131"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down
4 changes: 3 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "graph.hpp"

int main(int argc, const char* argv[]) {
invoke_two_sum_ii();
invoke_two_sum();

// invoke_two_sum_ii();

// invoke_find_three_number_sum();

Expand Down
18 changes: 18 additions & 0 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,3 +1442,21 @@ void invoke_two_sum_ii() {
std::cout<<number<<", ";
});
}

std::vector<int> two_sum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> hash_map;
for(int index = 0; index < nums.size(); ++index) {
auto diff = target - nums[index];
if(hash_map.count(diff)) {
return {hash_map[diff], index};
}
hash_map[nums[index]] = index;
}

return {};
}
void invoke_two_sum() {
std::vector<int> input = {2,7,11,15};
auto output = two_sum(input, 22);
std::cout<<"The indices of two sums are: "<<output[0]<<", "<<output[1]<<std::endl;
}
5 changes: 5 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,9 @@ void invoke_separate_black_and_white_balls();
*/
void invoke_two_sum_ii();

/*
1. Two Sum: https://leetcode.com/problems/two-sum/
*/
void invoke_two_sum();

#endif /* NUMBER_HPP */

0 comments on commit b6a69b7

Please sign in to comment.