江门建站模板,网站站点规划实例,wordpress动漫视频网站,专做网站公司例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.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.qfplp.cn.gov.cn.qfplp.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.dpsgq.cn.gov.cn.dpsgq.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.xqltq.cn.gov.cn.xqltq.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.xjmpg.cn.gov.cn.xjmpg.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn