广州市网站建设服务机构,做网站是数据库应该放在哪里,台州专业网站建设,网站界面可以做版权吗算法|数组——滑动窗口
引入
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl1, ..., numsr-1, numsr] #xff0c;并返回其长度**。**如果不存在符合条件的子数组#xff0c;返回 0 。
示例…算法|数组——滑动窗口
引入
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl1, ..., numsr-1, numsr] 并返回其长度**。**如果不存在符合条件的子数组返回 0 。
示例 1
输入target 7, nums [2,3,1,2,4,3]
输出2
解释子数组 [4,3] 是该条件下的长度最小的子数组。示例 2
输入target 4, nums [1,4,4]
输出1示例 3
输入target 11, nums [1,1,1,1,1,1,1,1]
输出0解法
暴力解法 class Solution {public int minSubArrayLen(int target, int[] nums) {int result Integer.MAX_VALUE;for(int i 0; i nums.length; i){int sum 0;for(int j i; j nums.length; j){sum nums[j];if(sum target){result Math.min(result,j - i 1);break;}}}return result Integer.MAX_VALUE ? 0 : result;}
}这种做法可以很容易想到可是谁想到它… 超时了哈哈 那么下面我们看看另外一种思路。
滑动窗口
先看示例代码
class Solution {public int minSubArrayLen(int target, int[] nums) {int result Integer.MAX_VALUE;int i 0;int sum 0;int length 0;for(int j 0; j nums.length; j){sum nums[j];while(sum target){length j - i 1;result Math.min(result,length);sum - nums[i];}}return result Integer.MAX_VALUE ? 0 : result;}
}下面见分析 还不错吧 至此先不更个1-2天哥们要考科四现在一题都没看再不看就寄了 文章转载自: http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.pwmm.cn.gov.cn.pwmm.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn