-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-Two-Sum.cpp
More file actions
35 lines (33 loc) · 920 Bytes
/
1-Two-Sum.cpp
File metadata and controls
35 lines (33 loc) · 920 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
34
35
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
map<int, vector<int>>m;
int idx = 0;
for( int i : nums )
m[i].push_back(idx++);
vector<int>ans;
for( int i = 0; i < nums.size(); i++ )
{
if( m[target - nums[i]].size() )
{
if( target - nums[i] != nums[i] )
{
ans.push_back(i);
ans.push_back(m[target - nums[i]][0]);
break;
}
else
{
if( m[target - nums[i]].size() > 1 )
{
ans.push_back(m[target - nums[i]][0]);
ans.push_back(m[target - nums[i]][1]);
break;
}
}
}
}
return ans;
}
};