微网站建设加盟,wordpress怎么增加语言,百度网站建设的十一个,wordpress 下划线1004. 最大连续1的个数 III 1004. 最大连续1的个数 III
题目描述#xff1a;
给定一个二进制数组 nums 和一个整数 k#xff0c;如果可以翻转最多 k 个 0 #xff0c;则返回 数组中连续 1 的最大个数 。 解题思路#xff1a; 首先题目要我们求出的最多翻转k个0后#x… 1004. 最大连续1的个数 III 1004. 最大连续1的个数 III
题目描述
给定一个二进制数组 nums 和一个整数 k如果可以翻转最多 k 个 0 则返回 数组中连续 1 的最大个数 。 解题思路 首先题目要我们求出的最多翻转k个0后可以翻转【0k】个0不一定要全翻转的连续1最多的子数组的长度 我们可以用leftright滑动窗口的思想定一个zero来记录0的个数right不断向右走当遇到nums【right】等于0时zero当zero的个数大于k的时候left先右走当遇到nums【left】等于0时zero--直到zerok,然后更新length的大小 解题代码
class Solution {
public:int longestOnes(vectorint nums, int k) {int length0;int nnums.size();int zero0;int left0,right0;while(rightn){if(nums[right]0)zero;while(zerok){if(nums[left]0)zero--;}lengthmax(length,right-left1);right;}return length;}
}; 1658. 将 x 减到 0 的最小操作数
1658. 将 x 减到 0 的最小操作数
题目描述
给你一个整数数组 nums 和一个整数 x 。每一次操作时你应当移除数组 nums 最左边或最右边的元素然后从 x 中减去该元素的值。请注意需要 修改 数组以供接下来的操作使用。
如果可以将 x 恰好 减到 0 返回 最小操作数 否则返回 -1 。 解题思路 本题是要不断从左右两边减一个数使x减为0我们可以发现左右两边减的数组是两个连续区间也就是说整个大数组被分成了三个小数组我们可以转换一下思想变为求中间数组之和等于target大数组之和-x的最长长度也就是变成了子数组问题 值得注意的是length应该初始化为-1而不是0因为当length0有两种情况 当数组为【5,6,7,8,9】而x4中间数组每个数都大于x刚好length0每个元素都要出的情况 解题代码
class Solution {
public:int minOperations(vectorint nums, int x) {int sum0;for(int i0;inums.size();i)sumnums[i];int targetsum-x;if(target0) return -1;int nnums.size();int length-1;for(int left0,right0,num0;rightn;right){numnums[right];while(numtarget)num-nums[left];if(numtarget)lengthmax(length,right-left1);}if(length-1)return length;else return n-length;}
};
文章转载自: http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn