当前位置:网站首页>数组——27. 移除元素
数组——27. 移除元素
2022-07-23 19:17:00 【向着百万年薪努力的小赵】
1 题目描述
- 移除元素
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}
2 题目示例
示例 1:
输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
示例 2:
输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,4,0,3]
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
3 题目提示
- 0 <= nums.length <= 100
- 0 <= nums[i] <= 50
- 0 <= val <= 100
4 思路
有的同学可能说了,多余的元素,删掉不就得了。
要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。
5 我的答案
class Solution {
public int removeElement(int[] nums, int val) {
// 快慢指针
int fastIndex = 0;
int slowIndex;
for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
if (nums[fastIndex] != val) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
}
边栏推荐
- 百度地图数据可视化
- 能量原理与变分法笔记14:总结+问题的求解
- [unity project practice] entrustment
- Baidu map data visualization
- 【ASP.NET Core】选项模式的相关接口
- Energy principle and variational method note 19: minimum complementary energy principle + possible work principle
- [PM2] PM2 common commands
- phar反序列化
- Non local mean filtering / attention mechanism
- Energy principle and variational method note 12: minimum potential energy principle
猜你喜欢

Redux summation case explanation version tutorial

C language leak detection and filling (1)

【ASP.NET Core】选项模式的相关接口

【无标题】

梅科尔工作室-华为14天鸿蒙设备开发实战笔记四

百度地图数据可视化

深入浅出边缘云 | 1. 概述

Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible

Top ten shrinking industries in China in the first half of 2022

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap
随机推荐
非局部均值滤波(NON-LOCAL-mean)/注意力机制
Baidu map data visualization
Leetcode 228. summary interval (yes, solved)
Attack and defense world web question Fakebook
How to add to-do items for win11 widgets? Win11 method of adding to-do widget
Mecol Studio - Little Bear Development Notes 3
剑指 Offer II 115. 重建序列
【C语言】通讯录(静态版本)
Leetcode 151. 颠倒字符串中的单词
Detailed writing process of impala
Energy principle and variational method note 17: generalized variational principle (identification factor method)
新品上市|A股场内衍生品大盘点
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
AtCoder——Subtree K-th Max
Leetcode 238. 除自身以外数组的乘积
Leetcode 228. 汇总区间(可以,已解决)
Top ten shrinking industries in China in the first half of 2022
牛客C基础题练习
Leetcode 219. duplicate Element II exists (yes, resolved)
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4