Leetcode Brushing Notes--Hot31-40

1--color classification (75)

Main ideas:

        quick row

#include <iostream>
#include <vector>

class Solution {
public:
    void sortColors(std::vector<int>& nums) {
        quicksort(nums, 0, nums.size()-1);
    }

    void quicksort(std::vector<int>& nums, int left, int right){
        if(left >= right) return;
        int pivot = nums[left];
        int l = left, r = right;
        while(l < r){
            while(l < r && nums[r] >= pivot) r--;
            nums[l] = nums[r];
            while(l < r && nums[l] <= pivot) l++;
            nums[r] = nums[l];
        }
        nums[l] = pivot;
        quicksort(nums, left, l-1);
        quicksort(nums, l+1, right);
    }
};

int main(int argc, char *argv[]){
    // nums = [2,0,2,1,1,0]
    std::vector<int> test = {2, 0, 2, 1, 1, 0};
    Solution S1;
    S1.sortColors(test);
    for(auto item : test){
        std::cout << item << " ";
    }
    return 0;
}

Main ideas: 

#include <iostream>
#include <vector>

class Solution {
public:
    void sortColors(std::vector<int>& nums) {
        int n = nums.size();
        int p0 = 0, p2 = n - 1;
        for (int i = 0; i <= p2; ++i) {
            while (i <= p2 && nums[i] == 2) {
                std::swap(nums[i], nums[p2]);
                --p2;
            }
            if (nums[i] == 0) {
                std::swap(nums[i], nums[p0]);
                ++p0;
            }
        }
    }
};

int main(int argc, char *argv[]){
    // nums = [2,0,2,1,1,0]
    std::vector<int> test = {2, 0, 2, 1, 1, 0};
    Solution S1;
    S1.sortColors(test);
    for(auto item : test){
        std::cout << item << " ";
    }
    return 0;
}

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Main ideas:

        Reference idea: video explanation

#include <iostream>
#include <string>
#include <unordered_map>

class Solution {
public:
    std::unordered_map<char, int> t_map;
    std::unordered_map<char, int> min_map;
public:
    std::string minWindow(std::string s, std::string t) {
        if(s.length() < t.length()) return "";
        for(int i = 0; i < t.length(); i++){
            if(t_map.find(t[i]) == t_map.end()) t_map[t[i]] = 1;
            else t_map[t[i]] += 1; 
        }
        int l = 0, r = 0;
        int min_l = 0, min_len = s.length() + 1;
        while(r < s.length()){
            if(min_map.find(s[r]) == min_map.end()) min_map[s[r]] = 1;
            else min_map[s[r]] += 1;
            while(check()){
                if(r - l + 1 < min_len){
                    min_l = l;
                    min_len = r - l + 1;
                }
                min_map[s[l]]--;
                l++; // 左指针右移
            }
            r++;
        }
        return min_len == s.length() + 1 ? "" : s.substr(min_l, min_len);
    }
    bool check(){
        if(t_map.size() > min_map.size()) return false;
        for(auto kv : t_map){
            char key = kv.first;
            int value = kv.second;
            if(min_map.find(key) == min_map.end() || min_map[key] < t_map[key]){
                return false;
            }
        }
        return true;
    }
};

int main(int argc, char *argv[]){
    // s = "ADOBECODEBANC", t = "ABC"
    std::string s = "ADOBECODEBANC";
    std::string t = "ABC";
    Solution S1;
    std::string res = S1.minWindow(s, t);
    std::cout << res << std::endl;
    return 0;
}

おすすめ

転載: blog.csdn.net/weixin_43863869/article/details/132502627