-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo1_TwoSum.cpp
64 lines (56 loc) · 1.28 KB
/
No1_TwoSum.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//No.1 Two Sum
//hashmap there is no duplicate
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hashmap;
vector<int> res;
int len = nums.size();
for(int i=0; i<len; i++){
// find if there is an element in the hashmap
int pair = target - nums[i];
if(hashmap.find(pair) != hashmap.end()){
res.push_back(hashmap[pair]);
res.push_back(i);
}
hashmap[nums[i]] = i;
}
return res;
}
};
// sort and two pointers, it could have duplicates
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
vector<int> tmp(nums);
sort(nums.begin(), nums.end());
int front = 0, end = nums.size() - 1;
while (front < end) {
if (nums[front] + nums[end] == target) {
break;
}
else if (nums[front] + nums[end] > target) {
end--;
}
else {
front++;
}
}
// in order to handle the duplicates
for (int i = 0; i < tmp.size(); i++) {
if (nums[front] == tmp[i]) {
res.push_back(i);
break;
}
}
for (int i = tmp.size()-1; i >= 0; i--) {
if (nums[end] == tmp[i]) {
res.push_back(i);
break;
}
}
return res;
}
};
// hashmap, duplicates