算法专题:滑动窗口

news/2024/5/20 9:21:04 标签: 算法, leetcode, 滑动窗口

参考练习习题总集

文章目录

  • 3. 无重复字符的最长子串
  • 30. 串联所有单词的子串
  • 76. 最小覆盖子串
  • 187. 重复的DNA序列
  • 219. 存在重复元素 II
  • 220. 存在重复元素 III
  • 396. 旋转函数
  • 424. 替换后的最长重复字符
  • 438. 找到字符串中所有字母异位词

滑动窗口太简单了,没啥说的自己做吧。

3. 无重复字符的最长子串

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if (s.size()==0) return 0;
        unordered_map<char,int> zd;zd[s[0]]=0;int max_len=1;
        for (int i=1;i<s.size();i++)
            if (zd.find(s[i])==zd.end()) zd[s[i]]=i;
            else
            {
                int x=my_min(zd);if (i-x>max_len) max_len=i-x;
                dlt(zd,zd[s[i]]);zd[s[i]]=i;
            }
        if (zd.size()>max_len) max_len=zd.size();
        return max_len;
    }
    int my_min(unordered_map<char,int> & zd)
    {
        int x=5*pow(10,4)+1;
        for (unordered_map<char,int>::iterator zz=zd.begin();zz!=zd.end();zz++)
            if (zz->second<x) x=zz->second;
        return x;
    }
    void dlt(unordered_map<char,int> & zd,int x)
    {
        vector<char> lt;
        for (unordered_map<char,int>::iterator zz=zd.begin();zz!=zd.end();zz++)
            if (zz->second<x) lt.push_back(zz->first);
        for (vector<char>::iterator zz=lt.begin();zz!=lt.end();zz++)
            zd.erase(*zz);
    }
};

30. 串联所有单词的子串

class Solution {
public:
    int word_len;unordered_map<string,int> zd;
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> result;
        int length=words.size()*words[0].size();
        if (s.size()<length) return result;
        unordered_map<char,int> zd1,zd2;
        for (int i=0;i<words.size();i++)
            for (int j=0;j<words[i].size();j++)
            {
                if (zd1.find(words[i][j])==zd1.end()) zd1[words[i][j]]=0;
                zd1[words[i][j]]+=1;
            }
        for (int i=0;i<length;i++)
        {
            if (zd2.find(s[i])==zd2.end()) zd2[s[i]]=0;
            zd2[s[i]]+=1;
        }
        word_len=words[0].size();
        for (int i=0;i<words.size();i++)
        {
            if (zd.find(words[i])==zd.end()) zd[words[i]]=0;
            zd[words[i]]+=1; 
        }
        string temp;
        temp=s.substr(0,length);
        if (zd1==zd2 and func(temp,zd)) result.push_back(0);
        for (int i=1;i<s.size()-length+1;i++)
        {
            zd2[s[i-1]]-=1;if (zd2[s[i-1]]==0) zd2.erase(s[i-1]);
            if (zd2.find(s[i+length-1])==zd2.end()) zd2[s[i+length-1]]=0;zd2[s[i+length-1]]+=1;
            temp=s.substr(i,length);
            if (zd1==zd2 and func(temp,zd)) result.push_back(i);
        }
        return result;
    }
    bool func(const string & s,unordered_map<string,int> zd)
    {
        for (int i=0;i<s.size();i+=word_len)
        {
            string temp=s.substr(i,word_len);
            if (zd.find(temp)==zd.end()) return false;
            zd[temp]-=1;if (zd[temp]==0) zd.erase(temp);
        }
        return true;
    }
};

76. 最小覆盖子串

class Solution {
public:
    string minWindow(string s, string t) {
        string result;
        unordered_map<char,int> zd;
        for (int i=0;i<t.size();i++)
        {
            if (zd.find(t[i])==zd.end()) zd[t[i]]=0;
            zd[t[i]]+=1;
        }
        vector<pair<char,int>> lb;
        for (int i=0;i<s.size();i++)
            if (zd.find(s[i])!=zd.end())
                lb.push_back({s[i],i});
        if (lb.size()<t.size()) return result;
        for (int i=0;i<t.size();i++)
            zd[lb[i].first]-=1;
        int l=0,r=t.size();
        while (!check(zd))
        {
            if (r>=lb.size()) return result;
            zd[lb[r].first]-=1;r+=1;
        }
        while (zd[lb[l].first]+1<=0)
        {
            zd[lb[l].first]+=1;l+=1;
        }
        result=s.substr(lb[l].second,lb[r-1].second-lb[l].second+1);
        while (1)
        {
            zd[lb[l].first]+=1;l+=1;
            while (zd[lb[l-1].first]>0)
            {
                if (r>=lb.size()) return result;
                zd[lb[r].first]-=1;r+=1;
            }
            if (lb[r-1].second-lb[l].second+1<result.size())
                result=s.substr(lb[l].second,lb[r-1].second-lb[l].second+1);
        }
        return result;
    }
    bool check(const unordered_map<char,int> & zd)
    {
        for (auto zz=zd.begin();zz!=zd.end();zz++)
            if (zz->second>0) return false;
        return true;
    }
};

187. 重复的DNA序列

class Solution {
public:
    vector<string> findRepeatedDnaSequences(string s) {
        vector<string> lb;
        if (s.size()<=10) return lb;
        unordered_map<string,int> zd;
        string s_new=s.substr(0,10);
        zd[s_new]=1;
        for (int i=10;i<s.size();i++)
        {
            s_new.erase(0,1);s_new+=s[i];
            if (zd.find(s_new)==zd.end()) zd[s_new]=1;
            else
            {
                if (zd[s_new]==1) lb.push_back(s_new);
                zd[s_new]+=1;
            }
        }
        return lb;
    }
};

219. 存在重复元素 II

直接暴力

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        for (int i=0;i<nums.size();i++)
            for (int j=i+1;j<=i+k and j<nums.size();j++)
                if (nums[i]==nums[j]) return 1;
        return 0;
    }
};

220. 存在重复元素 III

首次碰见这种思想,我就直接抄的题解
又学到了一种思想

class Solution {
public:
    int size;
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
        size=valueDiff+1;unordered_map<int,int> zd;
        for (int i=0;i<nums.size();i++) {
            int u=nums[i],idx=getIdx(u);
            if (zd.find(idx)!=zd.end()) return true;
            int l=idx-1,r=idx+1;
            if (zd.find(l)!=zd.end() and abs(u-zd[l])<=valueDiff) return true;
            if (zd.find(r)!=zd.end() and abs(u-zd[r])<=valueDiff) return true;
            zd[idx]=u;
            if (i>=indexDiff) zd.erase(getIdx(nums[i-indexDiff]));
        }
        return false;
    }
    int getIdx(int u) {
        return u>=0?u/size:(u+1)/size-1;
    }
};

396. 旋转函数

class Solution {
public:
    int maxRotateFunction(vector<int>& nums) {
        int count=0;
        for (int x:nums) count+=x;
        int f0=0;
        for (int i=0;i<nums.size();i++)
            f0+=i*nums[i];
        int result=f0;
        for (int i=1;i<nums.size();i++)
        {
            f0=f0+count-nums[nums.size()-i]*nums.size();
            if (f0>result) result=f0;
        }
        return result;
    }
};

424. 替换后的最长重复字符

我好菜啊

class Solution {
public:
    int characterReplacement(string s, int k) {
        int l=0,r=0,result=-1,result_temp;
        unordered_map<char,int> zd;
        while (r!=s.size())
        {
            while (1)
            {
                if (r-l<k+1) break;
                char temp=find_max_char(zd);
                if (r-l-zd[temp]<k) break;
                if (s[r]==temp) break;
                l+=1;zd[s[l-1]]-=1;
                if (zd[s[l-1]]==0) zd.erase(s[l-1]);
            }
            if (zd.find(s[r])==zd.end()) zd[s[r]]=0;
            zd[s[r]]+=1;
            r+=1;
            result_temp=r-l;
            if (result_temp>result) result=result_temp;
        }
        return result;
    }
    char find_max_char(unordered_map<char,int> & zd)
    {
        char result=zd.begin()->first;
        for (auto zz=++zd.begin();zz!=zd.end();zz++)
            if (zz->second>zd[result])
                result=zz->first;
        return result;
    }
};

438. 找到字符串中所有字母异位词

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> lb;
        if (s.size()<p.size()) return lb;
        unordered_map<char,int> zd1,zd2;
        for (int i=0;i<p.size();i++)
        {
            if (zd1.find(p[i])==zd1.end()) zd1[p[i]]=0;
            zd1[p[i]]+=1;
            if (zd2.find(s[i])==zd2.end()) zd2[s[i]]=0;
            zd2[s[i]]+=1;
        }
        if (zd1==zd2) lb.push_back(0);
        for (int i=1;i<s.size()-p.size()+1;i++)
        {
            zd2[s[i-1]]-=1;
            if (zd2[s[i-1]]==0) zd2.erase(s[i-1]);
            if (zd2.find(s[i-1+p.size()])==zd2.end()) zd2[s[i-1+p.size()]]=0;
            zd2[s[i-1+p.size()]]+=1;
            if (zd1==zd2) lb.push_back(i);
        }
        return lb;
    }
};

就做到这里吧。


http://www.niftyadmin.cn/n/5373197.html

相关文章

HCIA-HarmonyOS设备开发认证V2.0-3.2.轻量系统内核基础-时间管理

目录 一、时间管理1.1、时间接口 一、时间管理 时间管理以系统时钟为基础&#xff0c;给应用程序提供所有和时间有关的服务。系统时钟是由定时器/计数器产生的输出脉冲触发中断产生的&#xff0c;一般定义为整数或长整数。输出脉冲的周期叫做一个“时钟滴答”。系统时钟也称为…

【北邮鲁鹏老师计算机视觉课程笔记】04 fitting 拟合

【北邮鲁鹏老师计算机视觉课程笔记】04 fitting 拟合 1 拟合的任务 如何从边缘找出真正的线&#xff1f; 存在问题 ①噪声 ②外点、离群点 ③缺失数据 2 最小二乘 存在的问题 3 全最小二乘 度量的是点到直线的距离而不是点在y方向到直线的距离 提示&#xff1a;点到直线的…

操作字符串之子串削除-10-${string%substring}

1.${string%substring} 从$string的结尾位置截掉最短匹配的$substring 2.实例 操作字符串样例&#xff1a;string123ABCabc456xyzabc 字符串操作默认从右边开始进行 命令&#xff1a; echo ${string%a*c} [rootkibana ~]# echo ${string%a*c} 123ABCabc456xyz #从$strin…

C语言冒泡排序介绍

冒泡排序&#xff08;Bubble Sort&#xff09;是一种简单的排序算法&#xff0c;它重复地遍历待排序的数列&#xff0c;一次比较两个元素&#xff0c;如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换&#xff0c;也就是说该数列已经排序完成…

NSB_Login

1.访问界面 2.查看源码&#xff0c;发现提示爆破字典 3.下载字典 https://github.com/brannondorsey/naive-hashcat/releases/download/data/rockyou.txt4.burp进行爆破。&#xff08;字典有点大&#xff0c;直接裂开。&#xff09; 5.爆破成功&#xff0c;密码 scream &am…

政安晨:政安晨:机器学习快速入门(三){pandas与scikit-learn} {模型验证及欠拟合与过拟合}

这一篇中&#xff0c;咱们使用Pandas与Scikit-liarn工具进行一下模型验证&#xff0c;之后再顺势了解一些过拟合与欠拟合&#xff0c;这是您逐渐深入机器学习的开始&#xff01; 模型验证 评估您的模型性能&#xff0c;以便测试和比较其他选择。 在上一篇中&#xff0c;您已经…

机器学习---学习与推断,近似推断、话题模型

1. 学习与推断 基于概率图模型定义的分布&#xff0c;能对目标变量的边际分布&#xff08;marginal distribution&#xff09;或某些可观测变量 为条件的条件分布进行推断。对概率图模型&#xff0c;还需确定具体分布的参数&#xff0c;称为参数估计或学习问 题&#xff0c;…

SpringBoot 事务的属性rollbackFor 与 propagetion

rollbackFor介绍 默认情况下&#xff0c;只有出现 RuntimeException 才回滚异常。rollbackFor属性用于控制出现何种异常类型&#xff0c;回滚事务。 OverrideTransactionalpublic void insert() {classesMapper.delete(1);//删除班级int n 1/0;//发送运行时异常&#xff0c;数…