-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathTwo_sum.cpp
More file actions
23 lines (16 loc) · 713 Bytes
/
Two_sum.cpp
File metadata and controls
23 lines (16 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*Problem Statement: Given an array of integers nums[] and an integer target, return indices of the two numbers such that their sum is equal to the target.
Note: Assume that there is exactly one solution, and you are not allowed to use the same element twice.
Example: If target is equal to 6 and num[1] = 3, then nums[1] + nums[1] = target is not a solution. */
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> mp;
for (int i = 0; i < nums.size(); ++i) {
if (mp.find(target - nums[i]) != mp.end()) {
res.emplace_back(i);
res.emplace_back(mp[target - nums[i]]);
return res;
}
mp[nums[i]] = i;
}
return res;
}