【LeetCode】HOT 100(12)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Title: 75. Color Classification - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 76. Minimum Covering Substring - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Title: 75. Color Classification - Leetcode

The interface of the topic:

class Solution {
public:
    void sortColors(vector<int>& nums) {

    }
};

Problem-solving ideas:

The easiest way to this question is of course to sort directly:

class Solution {
public:
    void sortColors(vector<int>& nums) {
        sort(nums.begin(), nums.end());
    }
};

But the title says that we are not allowed to sort directly,

An advanced requirement is also put forward, let us complete the sorting by traversing once,

Then my idea is to traverse the array directly,

Put it at the front when you encounter 0, continue to go backward when you encounter 1, and put it at the end when you encounter 2.

We can use the right boundary as the condition for the end of our loop

code show as below:

code:

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        int i = 0;
        while(i <= right) {
            if(nums[i] == 0) {
                swap(nums[i], nums[left]);
                i++;
                left++;
            }
            else if(nums[i] == 1) i++;
            else { //nums[i] == 2
                swap(nums[i], nums[right]);
                right--;
            }
        }
    }
};

It's over! ! ! !

Title: 76. Minimum Covering Substring - Leetcode

The interface of the topic:

class Solution {
public:
    string minWindow(string s, string t) {

    }
};

Problem-solving ideas:

For this question, see the substring match, find the smallest substring,

I can't help but think of sliding windows,

This simplifies the problem to:

As long as we can find the rules for updating the boundary of the sliding window,

1. Find a matching letter and start updating the boundary,

2. Use count to record when the substring is matched,

3. When the substring is matched, update the left border.

Just install the above three-step idea to implement the code.

code show as below:

code:

class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char, int> need, window;
        for(auto& e : t) need[e]++; //需要匹配的子串进need哈希
        int left = 0, right = 0, len = INT_MAX, start = 0, count = 0;
        while(right < s.size()) { //遍历
            if(need.find(s[right]) != need.end()) { //如果找到能匹配的字母
                window[s[right]]++; //更新右边界
                if(window[s[right]] == need[s[right]]) { //记录需要匹配的子串匹配完了没
                    count++;
                }
            }
            while(count == need.size()) { //如果子串匹配完了
                if(len > right - left + 1) { //更新最短子串
                    start = left; //新最短子串的起始位置
                    len = right - left + 1; //新最短子串的长度
                }
                if(need.find(s[left]) != need.end()) { //如果还有左边界
                    if(need[s[left]] == window[s[left]]) { //且左边界是需要匹配的子串
                        count--; //减少计数,就能继续更新右边界了
                    }
                    window[s[left]]--; //更新左边界
                } 
                left++; //更新左边界
            }
            right++; //遍历
        }
        return len == INT_MAX ? "" : s.substr(start, len);
    }
};

It's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131102715