-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1.cpp
41 lines (36 loc) · 855 Bytes
/
p1.cpp
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
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
/*
for (int i = 0; i < nums.size(); i++)
{
int num = target - nums[i];
vector<int>::iterator it = find(nums.begin(), nums.end(), num);
int pos = distance(nums.begin(), it);
if (pos < nums.size() && pos != i)
{
return {nums[i], nums[pos]};
}
}
*/
// Usign Map
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++)
{
if (map[target - nums[i]] != 0)
{
return {map[target - nums[i]] - 1, i};
}
map[nums[i]] = i + 1;
}
return {0, 0};
}
};
int main()
{
return 0;
}