Article directory
1. 76. Minimum covering substring
1. Introduction to the topic
76. Minimum covering substring
You are given a string s and a string t. Returns the smallest substring in s that covers all characters of t. If there is no substring in s that covers all characters of t, the empty string "" is returned.
Note: For repeated characters in t, the number of characters in the substring we are looking for must be no less than the number of characters in t.
If such a substring exists in s, we guarantee that it is the only answer.
2. Problem-solving ideas
3.Code
class Solution {
public:
string minWindow(string s, string t) {
int n = s.size(), m = t.size();
vector<int> vt(128);
for(auto& e : t)
{
vt[e]++;
}
vector<int> vs(128, 0);
int count = 0;
int len = INT_MAX;
int begin = 0;
for(int left = 0, right = 0;right < n; ++right)
{
char t = s[right];
if(++vs[t] <= vt[t]) count++;
while(count == m)
{
len < (right - left + 1) ? len : (len = (right - left + 1), begin = left);
char t = s[left];
if(vs[t]-- <= vt[t]) count--;
left++;
}
}
if(len == INT_MAX) return "";
return s.substr(begin, len);
}
};
4. Running results
2. 704. Binary search
1. Introduction to the topic
704. Binary search
Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search for target in nums. If the target value exists, return the subscript, otherwise return -1.
2. Problem-solving ideas
3.Code
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while(left <= right)
{
int mid = left + (right - left) / 2;
if(nums[mid] < target)
{
left = mid + 1;
}
else if(nums[mid] > target)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
};
4. Running results
3. 34. Find the first and last position of an element in a sorted array
1. Introduction to the topic
34. Find the first and last position of an element in a sorted array.
You are given an array of integers nums arranged in non-decreasing order, and a target value target. Please find the starting position and ending position of the given target value in the array.
If the target value target does not exist in the array, [-1, -1] is returned.
You must design and implement an algorithm with time complexity O(log n) to solve this problem.
2. Problem-solving ideas
3.Code
class Solution {
public:
int _bsearch(vector<int>& nums, int target)
{
int left = 0, right = nums.size() - 1;
while(left <= right)
{
int mid = left + (right - left) / 2;
if(nums[mid] < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return right + 1;
}
int _esearch(vector<int>& nums, int target)
{
int left = 0, right = nums.size() - 1;
while(left <= right)
{
int mid = left + (right - left) / 2;
if(nums[mid] <= target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return left - 1;
}
vector<int> searchRange(vector<int>& nums, int target) {
int begin = _bsearch(nums, target);
int end = _esearch(nums, target);
if(begin <= end && end < nums.size() && begin >= 0)
return {
begin, end};
return {
-1, -1};
}
};
4. Running results
Summarize
Today is the 6th day of algorithm practice.
If you persevere, you can carve gold and stone , keep working hard.
Source: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!