-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-Two-Sum.cpp
More file actions
26 lines (24 loc) · 751 Bytes
/
1-Two-Sum.cpp
File metadata and controls
26 lines (24 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// # 1. Two Sum https://leetcode.com/problems/two-sum/
// # leve: easy
// # complexity: Time O(n), Space: O(n)
// """
// Explaination:
// brute force would be checking each value in the list and the ones that follows, to see if they sum up to target value.
// The optimal solution is to use a hashmap
// """
// rewriting my python code in cpp to brush up my cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int>map;
for(int i = 0; i < nums.size(); i++){
if(map.find(target-nums[i]) != map.end()){
return {i, map[target - nums[i]]};
}
else{
map[nums[i]] = i;
}
}
return {};
}
};