-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum.cpp
More file actions
33 lines (30 loc) · 787 Bytes
/
Copy pathtwo_sum.cpp
File metadata and controls
33 lines (30 loc) · 787 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
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
Solution(){
cout << "Bulid Solution!" << endl;
}
vector<int> twoSum(vector<int>& nums, int target){
unordered_map<int, int> umap;
vector<int> ans;
for (int i = 0; i < nums.size(); i++){
if (umap.count(nums[i])){
ans.push_back(umap[nums[i]]);
ans.push_back(i);
break;
}
umap[target-nums[i]] = i;
}
return ans;
}
};
int main(){
vector<int> arr = {3,2,4};
Solution u_solver;
vector<int> ans = u_solver.twoSum(arr, 6);
for (int i : ans){
cout << i << endl;
}
return 0;
}