江门建站模板,大型网站开发工具,电子商务专业就业方向及就业前景,天津市区县档案部门网站建设指导意见例1
209. 长度最小的子数组 ①窗口大小不固定
②求最小长度 - ret INT_MAX
③数组内的值都大于0#xff0c; 符合单调性#xff08;sum nums[right] - sum增大#xff09;
while里面符合条件#xff0c;在里面更改ret 参考代码
class Solution {
public:i…例1
209. 长度最小的子数组 ①窗口大小不固定
②求最小长度 - ret INT_MAX
③数组内的值都大于0 符合单调性sum nums[right] - sum增大
while里面符合条件在里面更改ret 参考代码
class Solution {
public:int minSubArrayLen(int target, vectorint nums) {int ret INT_MAX;for(int left 0, right 0, sum 0; right nums.size(); right){sum nums[right];while(sum target){ret min(ret, right - left 1);sum - nums[left];}}return ret INT_MAX ? 0 : ret;}
};
例2
3. 无重复字符的最长子串 while里面是不符合条件的外面与ret比较就行
参考代码
class Solution {
public:int lengthOfLongestSubstring(string s) {int hash[128] {0};int ret 0;for(int left 0, right 0; right s.size(); right){hash[s[right]];while(hash[s[right]] 1){hash[s[left]]--;}ret max(ret, right - left 1);}return ret;}
};
例3
1004. 最大连续1的个数 III [right, left]有效闭区间
参考代码
class Solution {
public:int longestOnes(vectorint nums, int k) {int ret 0;for(int left 0, right 0, zero 0; right nums.size(); right){if(nums[right] 0) zero;while(zero k){if(nums[left] 0) zero--;}ret max(ret, right - left 1);}return ret;}
};
例4 转换为求中间最大长度
如果要用注释掉的部分就要写上target 0因为while(tmp target) 会left这里的会导致left越界所以分开写较好把满足条件的放在外面
参考代码
class Solution {
public:int minOperations(vectorint nums, int x) {int sum 0, ret -1;for(auto e : nums)sum e;int target sum - x;if(target 0) return -1;// if(target 0) return nums.size();//for(int left 0, right 0, tmp 0; right nums.size(); right){tmp nums[right];// while(tmp target)//☆☆☆☆☆// {// if(tmp target)// ret max(ret, right - left 1);// tmp - nums[left];//的时候越界最后一次// }while(tmp target) tmp - nums[left];if(tmp target) ret max(ret, right - left 1);}return ret -1 ? -1 : nums.size() - ret;}
};例5
904. 水果成篮 后面的题都用到哈希映射
分析哈希的临界点是从0 - 1 和从 1 - 0
语法--hash[fruits[left]] 看括号里面的优先外面括号的前置“”“--” 往后稍稍所以hash的索引是fruit[left]再是left自增再是--hash[fruit[left]]
参考代码
class Solution {
public:int totalFruit(vectorint fruits) {int hash[100001] {0}, ret 0;for(int left 0 ,right 0 ,count 0; right fruits.size(); right){if(hash[fruits[right]] 0) count;while(count 2)if(--hash[fruits[left]] 0) count--;ret max(ret, right - left 1);}return ret;}
};
例6
438. 找到字符串中所有字母异位词 分析因为是固定窗口所以if(right - left 1 p.size())用的是if只用右移一次left
语法分析
①代码是全都拆开
②后置 和后置 -- 在这里两个写一起是不对的因为右操作数例有left顺序是这样的显示返回hash2[s[left],然后left然后hash2[s[left]]--这时候left已经变大了1导致左右两边left不是相同的值
③和⑤可以统一left
②③和④⑤是后置-- 和前置-- 的区别所以判断条件也会不同。个人觉得后置的更好直接理解
参考代码
class Solution {
public:vectorint findAnagrams(string s, string p) {vectorint ret;int hash1[128] {0}, hash2[128] {0};for(auto e : p)hash1[e];for(int left 0, right 0, count 0; right s.size(); right){if(hash2[s[right]] hash1[s[right]]) count;if(right - left 1 p.size()){// if(hash2[s[left]] hash1[s[left]]) count--; 1// hash2[s[left]]--;// left;//if(hash2[s[left]]-- hash1[s[left]]) count--;不对先 2// if(hash2[s[left]]-- hash1[s[left]]) count--; 3// left;//if(--hash2[s[left]] hash1[s[left]]) count--;//不行 4//这里的后缀比前置的--优先级高if(--hash2[s[left]] hash1[s[left]]) count--; //5left;}if(count p.size())ret.push_back(left);}return ret;}
};
例7 分析对比上题就是把字符换成了字符串那就只能用unordered_mapstring, int,
题目说了words里面的每个元素的长度相同次数words[0].size()
注意 leftright i不是0不然会ret是重复的数组
对于这行代码 if(hash1.count(in) hash2[in] hash1[in]) count;如果hash1[in]没有这个in那么会自己创建一个会浪费时间前面加上hash1.count(in)判断可以减少时间的消耗
参考代码
class Solution {
public:vectorint findSubstring(string s, vectorstring words) {unordered_mapstring, int hash1;vectorint ret;for(auto e : words)hash1[e];int len words[0].size();for(int i 0; i len; i){unordered_mapstring, int hash2;for(int left i, right i, count 0; right len s.size(); right len){string in(s.substr(right, len));if(hash1.count(in) hash2[in] hash1[in]) count;if(right - left len - 1 words.size() * len){string out(s.substr(left, len));if(hash1.count(out) hash2[out]-- hash1[out]) count--;left len;}if(count words.size())ret.push_back(left);} }return ret;}
};
例8
76. 最小覆盖子串 ①ret min(ret, right - left 1), begin left;
②if(right - left 1 ret) {ret right - left 1;begin left;}
①②两段代码是有区别的 上面不管ret是否变化begin就会改变 下面的ret变小了才会变化
依据上面的题目知道这里的left是不能写在里面的
if(hash2[s[left]]-- hash1[s[left]]) count--;left;
注这里是求最小长度那么窗口肯定是变化的肯定是while
参考代码
class Solution {
public:string minWindow(string s, string t) {int hash1[128] {0}, hash2[128] {0}, ret INT_MAX, begin -1;for(auto e : t)hash1[e];for(int left 0, right 0, count 0; right s.size(); right){if(hash2[s[right]] hash1[s[right]]) count;while(count t.size()){// ret min(ret, right - left 1), begin left;if(right - left 1 ret){ret right - left 1;begin left;}if(hash2[s[left]]-- hash1[s[left]]) count--;left;}}return ret INT_MAX ? : s.substr(begin, ret);}
};
文章转载自: http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.kltmt.cn.gov.cn.kltmt.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.bqqzg.cn.gov.cn.bqqzg.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.knjj.cn.gov.cn.knjj.cn