Skip to content

Commit 70ae377

Browse files
PDKhanriver20s
authored andcommitted
two-sum solution
1 parent 10aaefc commit 70ae377

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

two-sum/PDKhan.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int, int> Map;
5+
6+
for(int i = 0; i < nums.size(); i++){
7+
int diff = target - nums[i];
8+
9+
if(Map.find(diff) != Map.end()){
10+
return { Map[diff], i };
11+
}
12+
13+
Map[nums[i]] = i;
14+
}
15+
16+
return {};
17+
}
18+
};

0 commit comments

Comments
 (0)