茂名市电白区住房和城乡建设局网站,搭建系统,网站及管理系统,买网站主机给你一个数组 nums 和一个值 val#xff0c;你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。
假设 nums 中不等于 val 的元素数量为 k#xff0c;要通过此题#xff0c;您需要执行以下操作#xff1a;
更改…给你一个数组 nums 和一个值 val你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。
假设 nums 中不等于 val 的元素数量为 k要通过此题您需要执行以下操作
更改 nums 数组使 nums 的前 k 个元素包含不等于 val 的元素。nums 的其余元素和 nums 的大小并不重要。返回 k。
用户评测
评测机将使用以下代码测试您的解决方案
int[] nums [...]; // 输入数组
int val ...; // 要移除的值
int[] expectedNums [...]; // 长度正确的预期答案。// 它以不等于 val 的值排序。int k removeElement(nums, val); // 调用你的实现assert k expectedNums.length;
sort(nums, 0, k); // 排序 nums 的前 k 个元素
for (int i 0; i actualLength; i) {assert nums[i] expectedNums[i];
}
如果所有的断言都通过你的解决方案将会 通过。 示例 1
输入nums [3,2,2,3], val 3
输出2, nums [2,2,_,_]
解释你的函数函数应该返回 k 2, 并且 nums 中的前两个元素均为 2。
你在返回的 k 个元素之外留下了什么并不重要因此它们并不计入评测。
示例 2
输入nums [0,1,2,2,3,0,4,2], val 2
输出5, nums [0,1,4,0,3,_,_,_]
解释你的函数应该返回 k 5并且 nums 中的前五个元素为 0,0,1,3,4。
注意这五个元素可以任意顺序返回。
你在返回的 k 个元素之外留下了什么并不重要因此它们并不计入评测。提示
0 nums.length 1000 nums[i] 500 val 100
-------------------------------------------------------------------------------------- 自己解答
int removeElement(int* nums, int numsSize, int val)
{int index 0;for(int i 0;inumsSize;i){if(nums[i]!val){nums[index] nums[i];index;}}return index;
} 官方解答
int removeElement(int* nums, int numsSize, int val) {int left 0, right numsSize;while (left right) {if (nums[left] val) {nums[left] nums[right - 1];right--;} else {left;}}return left;
}作者力扣官方题解
链接https://leetcode.cn/problems/remove-element/solutions/730203/yi-chu-yuan-su-by-leetcode-solution-svxi/
来源力扣LeetCode
著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。
官方解答在每次遍历到符合条件的元素时从后往前使用数组尾部的元素覆盖掉该元素。这样减少了实际遍历的时间。 文章转载自: http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.gsqw.cn.gov.cn.gsqw.cn http://www.morning.nggry.cn.gov.cn.nggry.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.hyryq.cn.gov.cn.hyryq.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.ahlart.com.gov.cn.ahlart.com