Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions longest_substr_without_repeating_chars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <unordered_set>
using namespace std;

int lengthOfLongestSubstring(string s) {
unordered_set<char> seen;
int maxLen = 0, left = 0;
for (int right = 0; right < s.size(); right++) {
while (seen.find(s[right]) != seen.end()) {
seen.erase(s[left++]);
}
seen.insert(s[right]);
maxLen = max(maxLen, right - left);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::max is used without including . Add ' #include ' to this file.

}
return maxLen;
}
17 changes: 17 additions & 0 deletions merge_intervals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
vector<vector<int>> merged;
for (auto interval : intervals) {
if (merged.empty() || merged.back()[1] < interval[1]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition incorrectly handles interval merging; it should check if interval[0] <= merged.back()[1] to decide merging.

merged.push_back(interval);
} else {
merged.back()[1] = max(merged.back()[1], interval[1]);
}
}
return merged;
}
15 changes: 15 additions & 0 deletions peak_element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <vector>
using namespace std;

int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing nums[mid+1] may cause out-of-bounds if mid = nums.size() - 1. Revise the binary search to avoid undefined behavior.

right = mid;
else
left = mid + 1;
}
return left;
}