当前位置:网站首页>27.移除元素
27.移除元素
2022-07-26 10:42:00 【Forest_1010】
题目描述:
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
//使用两个下标,首先遍历数组
//如果不是目标值,就把元素向前覆盖到数组,相当于删除了元素
//返回下标
int index = 0;
for (int i = 0; i < nums.size(); ++i)
{
if (val != nums[i])
{
nums[index++] = nums[i];
}
}
return index;
}
};
边栏推荐
- Flutter CachedNetworkImage圆角
- 使用Geoprocessor 工具
- [dectectron2] follow the official demo
- 2021-08-13和鹏哥学C语言-数组
- Problems encountered in QRcode QR code (C language)
- [leetcode daily question 2021/5/8]1723. The shortest time to complete all work
- 构建ARM嵌入式开发环境
- 第7期:内卷和躺平,你怎么选
- 多目标优化系列1---NSGA2的非支配排序函数的讲解
- 第4期:大学生提前职业技能准备之一
猜你喜欢

Issue 5: the second essential skill for College Students

Anaconda is used on vscode (the environment has been configured)

oracle 启动不了 tnslistener服务启动不了

工厂模式详解

英语基础句型结构------起源

The problem of large fluctuation of hx711 data
![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】

The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly

从蚂蚁的觅食过程看团队研发(转载)

Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
随机推荐
Flutter集成极光推送
[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字
Parallelism, concurrency and several directions for high concurrency optimization
Issue 6: which mainstream programming language should college students choose
鹏哥C语言第七节课总结
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
Zongzi battle - guess who can win
GIS方法类期刊和论文的综述(Introduction)怎么写?
Error[pe147]: declaration is incompatible with 'error problem
10 令 operator= 返回一个 reference to *this
12 复制对象时勿忘其每一个成分
Simple use of json-c Library -- converting JSON files to struct
A semicolon is missing
访问权限——private,public,protected
Koin
13 managing resources by objects
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
Issue 7: how do you choose between curling up and lying flat
一文详解Nodejs中fs文件模块与path路径模块
如何实现临时的图形要素现实