张家港哪家做企业网站,中文com域名注册,微信商城在哪里进入,手机商城网站#x1f4d8;北尘_#xff1a;个人主页 #x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上#xff0c;不忘来时的初心 文章目录 一、水果成篮1、题目讲解2、讲解算法思路3、代码实现 二、找到字符串中所有字母异位词1、题目… 北尘_个人主页 个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上不忘来时的初心 文章目录 一、水果成篮1、题目讲解2、讲解算法思路3、代码实现 二、找到字符串中所有字母异位词1、题目讲解2、讲解算法思路3、代码实现 三、串联所有单词的子串1、题目讲解2、讲解算法思路3、代码实现 四、最小覆盖子串1、题目讲解2、讲解算法思路3、代码实现 一、水果成篮
1、题目讲解 2、讲解算法思路 3、代码实现
class Solution {
public:int totalFruit(vectorint f) {int nf.size();unordered_mapint,int hash;int ret0;for(int left0,right0;rightn;right){hash[f[right]];while(hash.size()2){hash[f[left]]--;if(hash[f[left]]0){hash.erase(f[left]);}left;}retmax(ret,right-left1);}return ret;}};二、找到字符串中所有字母异位词
1、题目讲解 2、讲解算法思路 3、代码实现
class Solution {
public:vectorint findAnagrams(string s, string p) {vectorint ret;int hash1[256]{0},lenp.size();for(char ch:p) hash1[ch];int hash2[256]{0};for(int left0,right0,count0;rights.size();right){char ins[right];hash2[in];if(hash2[in]hash1[in]) count;if(right-left1len){char outs[left];if(hash2[out]hash1[out]) count--;hash2[out]--;left;}if(countlen){ret.push_back(left);}}return ret; }
};三、串联所有单词的子串
1、题目讲解 2、讲解算法思路 3、代码实现
class Solution {
public:vectorint findSubstring(string s, vectorstring words) {vectorint ret;unordered_mapstring,int hash1;for(auto ch:words){hash1[ch];}int lenwords[0].size(),mwords.size();for(int i0;ilen;i){unordered_mapstring,int hash2;for(int lefti,righti,count0;rightlens.size();rightlen){string ins.substr(right,len);hash2[in];if(hash1.count(in) hash2[in]hash1[in]) count;if(right-left1len*m){string outs.substr(left,len);if(hash1.count(out) hash2[out]hash1[out]) count--;hash2[out]--;leftlen;}if(countm) ret.push_back(left);}}return ret;}
};四、最小覆盖子串
1、题目讲解 2、讲解算法思路 3、代码实现 代码一 class Solution {
public:string minWindow(string s, string t) {int hash1[256]{0};int kinds0;for(auto ch:t){if(hash1[ch]0) kinds;hash1[ch];}int hash2[256]{0};int minlenINT_MAX,begin-1;for(int left0,right0,count0;rights.size();right){char ins[right];hash2[in];if(hash2[in]hash1[in]) count;while(countkinds){if(right-left1minlen){minlenright-left1;beginleft;}char outs[left];if(hash2[out]--hash1[out]) count--; } }if(begin-1) return ;else return s.substr(begin,minlen);}
};代码二 不使用kinds来计算种类 class Solution {
public:string minWindow(string s, string t) {int hash1[256]{0},nt.size();for(char ch:t){hash1[ch];}int begin-1,lenINT_MAX;int hash2[256]{0};for(int left0,right0,count0;rights.size();right){char ins[right];hash2[in];if(hash2[in]hash1[in]) count;while(countn){if(right-left1len){beginleft;lenright-left1;}char outs[left];if(hash2[out]hash1[out]) count--;hash2[out]--;left;}}if(begin-1) return ;else return s.substr(begin,len);}}; 文章转载自: http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.bcjbm.cn.gov.cn.bcjbm.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.kmldm.cn.gov.cn.kmldm.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.wcrcy.cn.gov.cn.wcrcy.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.nmymn.cn.gov.cn.nmymn.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn