Skip to content

Commit 8c86a4c

Browse files
committed
two-sum solution
1 parent 83f9b70 commit 8c86a4c

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

contains-duplicate/dylan-jung.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
2-
public:
3-
bool containsDuplicate(vector<int>& nums) {
4-
unordered_set<int> s;
5-
for(auto item: nums) {
6-
if(s.count(item) > 0) return true;
7-
s.insert(item);
8-
}
9-
return false;
2+
public:
3+
bool containsDuplicate(vector<int>& nums) {
4+
unordered_set<int> s;
5+
for(auto item: nums) {
6+
if(s.count(item) > 0) return true;
7+
s.insert(item);
108
}
11-
};
9+
return false;
10+
}
11+
};

two-sum/dylan-jung.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+
vector<tuple<int, int>> arr;
5+
arr.reserve(nums.size());
6+
for(int i = 0; i < nums.size(); i++) {
7+
arr.push_back({nums[i], i});
8+
}
9+
int s = 0; int e = nums.size()-1;
10+
sort(arr.begin(), arr.end());
11+
while(s < e) {
12+
if(get<0>(arr[s]) + get<0>(arr[e]) > target) e-=1;
13+
else if (get<0>(arr[s]) + get<0>(arr[e]) < target) s+=1;
14+
else return {get<1>(arr[s]), get<1>(arr[e])};
15+
}
16+
return {};
17+
}
18+
};

0 commit comments

Comments
 (0)