Skip to content

Commit 3a24f13

Browse files
committed
Two sum solution added
0 parents  commit 3a24f13

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Two Sum.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int,int> m;
5+
vector<int> result;
6+
int n = nums.size();
7+
8+
for(int i = 0; i<n; i++)
9+
m.insert(make_pair(nums[i], i));
10+
11+
for(int i = 0; i<n; i++) {
12+
int key = target-nums[i];
13+
auto it = m.find(key);
14+
if(it != m.end() && it->second != i) {
15+
result.push_back(i);
16+
result.push_back(it->second);
17+
return result;
18+
}
19+
}
20+
21+
return result;
22+
23+
}
24+
};
25+

0 commit comments

Comments
 (0)