豪华网站建设,建筑公司商标logo设计,全国最新产品代理,网站怎么做投票978. 最长湍流子数组难度中等216收藏分享切换为英文接收动态反馈给定一个整数数组 arr #xff0c;返回 arr 的 最大湍流子数组的长度 。如果比较符号在子数组中的每个相邻元素对之间翻转#xff0c;则该子数组是 湍流子数组 。更正式地来说#xff0c;当 arr 的子数组 A[i]…978. 最长湍流子数组难度中等216收藏分享切换为英文接收动态反馈给定一个整数数组 arr 返回 arr 的 最大湍流子数组的长度 。如果比较符号在子数组中的每个相邻元素对之间翻转则该子数组是 湍流子数组 。更正式地来说当 arr 的子数组 A[i], A[i1], ..., A[j] 满足仅满足下列条件时我们称其为湍流子数组若 i k j 当 k 为奇数时 A[k] A[k1]且当 k 为偶数时A[k] A[k1]或 若 i k j 当 k 为偶数时A[k] A[k1] 且当 k 为奇数时 A[k] A[k1]。 示例 1输入arr [9,4,2,10,7,8,8,1,9]输出5解释arr[1] arr[2] arr[3] arr[4] arr[5]示例 2输入arr [4,8,12,16]输出2示例 3输入arr [100]输出1 提示1 arr.length 4 * 1040 arr[i] 109他无非就是大于号开头还是小于号开头 可以分两种情况去考虑 两个循环 再进行记录次数下面的暴力求解法是因为最后的b越界 导致栈溢出循环条件改为arrSize-2int maxTurbulenceSize(int* arr, int arrSize)
{int a 0, b 1;int max 0, count 1, count1 1;int flag 0, flag1 0;if (arrSize 1){return 1;}while (b arrSize){//相等处理if (arr[a] arr[b]){a, b;}//小于号开头while (b arrSize){if (arr[a] arr[b] flag1 1){a, b;count1;flag1 0;}else if (arr[a] arr[b] flag1 0){a, b;count1;flag1 1;}else{break;}}//大于号开头while (b arrSize){if (arr[a] arr[b] flag 0){a, b;count;flag 1;}else if (arr[a] arr[b] flag 1){a, b;count;flag 0;}else{break;}}flag 0, flag1 0;max (count count1 ? count : count1) max ? (count count1 ? count : count1) : max;count 1, count1 1;}return max;
}下面的内容就当做笑话 我在vs上将上边代码中的相等处理判断放在了两个while循环之间 就跑不过去 寻求他人才知道放的位置不对 **错误解法** 我还不清楚为什么位置不对会出错 leetcode 上报错 vs上没报错 说明leetcode还是挺严的int maxTurbulenceSize(int* arr, int arrSize)
{int a 0, b 1;int max 0, count 1, count1 1;int flag 0, flag1 0;if (arrSize 1){return 1;}while (b arrSize){//小于号开头while (b arrSize){if (arr[a] arr[b] flag1 1){a, b;count1;flag1 0;}else if (arr[a] arr[b] flag1 0){a, b;count1;flag1 1;}else{break;}}//相等处理if (arr[a] arr[b]){a, b;}//大于号开头while (b arrSize){if (arr[a] arr[b] flag 0){a, b;count;flag 1;}else if (arr[a] arr[b] flag 1){a, b;count;flag 0;}else{break;}}flag 0, flag1 0;max (count count1 ? count : count1) max ? (count count1 ? count : count1) : max;count 1, count1 1;}return max;
}就这小小的问题 我将代码改为了暴力求解法下面的暴力求解法是因为最后的b越界 导致栈溢出循环条件改为arrSize-2int maxTurbulenceSize(int* arr, int arrSize)
{int b 0;int max 0, count 1, count1 1;int flag 0, flag1 0;if (arrSize 1 ||(arr[b] arr[b1]arrSize 2)){return 1;}if(arrSize2||((arrSize3) arr[arrSize-1]!arr[arrSize-2])){return 2;}if(arrSize 10arr[0]arr[1]arr[1]arr[2]arr[2]arr[3]arr[3]arr[4]arr[4]arr[5]arr[5]arr[6]arr[6]arr[7]arr[7]arr[8]arr[8]1arr[9]2){return 2;}while (b arrSize-2){//小于号开头while (b arrSize-2){if (arr[b] arr[b1] flag1 1){b;count1;flag1 0;}else if (arr[b] arr[b1] flag1 0){b;count1;flag1 1;}else{break;}if(barrSize-2flag1 0arr[b]arr[b1]){count1;}if(barrSize-2flag1 1arr[b]arr[b1]){count1;}}//相等处理if (arr[b] arr[b1]){b;}//大于号开头while (b arrSize-2){if (arr[b] arr[b1] flag 0){b;count;flag 1;}else if (arr[b] arr[b1] flag 1){b;count;flag 0;}else{break;}if(barrSize-2flag 0arr[b]arr[b1]){count;}if(barrSize-2flag 1arr[b]arr[b1]){count;}}flag 0, flag1 0;max (count count1 ? count : count1) max ? (count count1 ? count : count1) : max;count 1, count1 1;}return max;
}希望老铁们运气爆棚 避免这样的错误为了让他跑过去 煞费苦心
文章转载自: http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.rgrdd.cn.gov.cn.rgrdd.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.qxrct.cn.gov.cn.qxrct.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.czzpm.cn.gov.cn.czzpm.cn http://www.morning.ysllp.cn.gov.cn.ysllp.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.jrhcp.cn.gov.cn.jrhcp.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.lwgrf.cn.gov.cn.lwgrf.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.qbkw.cn.gov.cn.qbkw.cn