北京网站设计外包公司,网站交易模块怎么做,制作网页推广宣传,任县网站制作2. 复写零 给你一个长度固定的整数数组 arr #xff0c;请你将该数组中出现的每个零都复写一遍#xff0c;并将其余的元素向右平移。 注意#xff1a;请不要在超过该数组长度的位置写入元素。请对输入的数组 就地 进行上述修改#xff0c;不要从函数返回任何东西。 示例 1请你将该数组中出现的每个零都复写一遍并将其余的元素向右平移。 注意请不要在超过该数组长度的位置写入元素。请对输入的数组 就地 进行上述修改不要从函数返回任何东西。 示例 1 输入arr [1,0,2,3,0,4,5,0]
输出[1,0,0,2,3,0,0,4]
解释调用函数后输入的数组将被修改为[1,0,0,2,3,0,0,4]示例 2 输入arr [1,2,3]
输出[1,2,3]
解释调用函数后输入的数组将被修改为[1,2,3]算法思路
本题使用双指针算法.
如果[从前往后]进行原地复写的话, 由于0会复写两次, 导致没有被复写的数被[覆盖]掉了. 因此我们使用[从后向前]的复写策略.
算法流程 初始化两个指针cur 0, dest -1 先找到最后一个复写的数, 使cur指向最后一个复写的数, dest指向从后往前复写的起始位置, 应该是数组的最后一个元素的位置. 当cur arr.length时, 一直执行下面的循环: 先判断cur位置的值 如果为0, dest向后移动2步如果不是0, dest向后移动1步 判断dest是否已经到达数组的最后一个元素的位置, 如果到达了, 就终止循环. cur, 继续判断 处理边界情况. 判断dest是否发生越界(dest arr.length): 如果发生了越界: 让数组arr[arr.length - 1] 0cur--dest - 2 从后向前完成复写操作, 只要cur 0 判断cur位置的值 如果是0: dest和dest - 1位置的值改为0, dest - 2如果不是0: dest位置的值改为cur位置的值, dest-- cur--
Java代码
class Solution {public static void duplicateZeros(int[] arr) {int cur 0, dest -1;while (cur arr.length) {if(arr[cur] 0) {dest 2;} else {dest;}if(dest arr.length - 1) {break;}cur;}if(dest arr.length) {arr[arr.length - 1] 0;dest - 2;cur--;}while (cur 0) {if(arr[cur] 0) {arr[dest--] 0;arr[dest--] 0;cur--;} else {arr[dest--] arr[cur--];}}}
}时间复杂度: O(N) 空间复杂度O(1) 文章转载自: http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.gqbks.cn.gov.cn.gqbks.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn