forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSbeo-Joe.cpp
More file actions
30 lines (28 loc) · 743 Bytes
/
Sbeo-Joe.cpp
File metadata and controls
30 lines (28 loc) · 743 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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for(int i = 0; i < nums.size() - 1; i++){
for(int j = i + 1; j < nums.size(); j++){
if(nums[i] + nums[j] == target){
return std::vector<int>{i, j};
}
}
}
return {};
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// value, index;
unordered_map<int, int> um;
for(int i = 0; i < nums.size(); i++){
int gap = target - nums[i];
if(um.count(gap)){
return {um[gap], i};
}
um[nums[i]] = i;
}
return {};
}
};