Skip to content

Commit 36e2d15

Browse files
committed
[1week]two-sum Sold
1 parent 6e6141d commit 36e2d15

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

two-sum/Hong-Study.cpp

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

0 commit comments

Comments
 (0)