-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwo Sum.cpp
38 lines (34 loc) · 1.05 KB
/
Two Sum.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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> m;
for(int i=0;i<nums.size();i++)
m[nums[i]]=i;
/* for(unordered_map<int,int> :: iterator itr=m.begin();itr!=m.end();itr++)
cout<<itr->first<<" "<<itr->second<<"\n";*/
vector<int> v;
for(int i=0;i<nums.size();i++)
{
int com=target-nums[i];
unordered_map<int,int> :: iterator itr=m.find(com);
if(itr!=m.end())
{
if(itr->second!=i)
{
if(i>itr->second)
{
v.push_back(itr->second);
v.push_back(i);
}
else
{
v.push_back(i);
v.push_back(itr->second);
}
break;
}
}
}
return v;
}
};