当前位置:网站首页>27. 移除元素
27. 移除元素
2022-06-25 06:41:00 【AlbertOS】
引入
给你一个数组 n u m s nums nums 和一个值 v a l val val,你需要 原地 移除所有数值等于 v a l val val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O ( 1 ) O(1) O(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],也会被视作正确答案。
输入: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。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
题解
这个用双指针覆盖你要删除的元素位置就好了,最后在修改数组的长度就好了,这里我使用容器vector存放数组,它可以智能调整数组长度,比自己调整方便~
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n = nums.size();
int left = 0;
for (int right = 0; right < n; right++) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
}
};
总结
其实今天做了一道困难题,但是还有待提高的地方,就不发那个了~
边栏推荐
- Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
- 國外LEAD域名郵箱獲取途徑
- VectorDraw Web Library 10.10
- MySQL facet 01
- JMeter introduction practice ----- use of global variables and local variables
- Runtime——methods成员变量,cache成员变量
- smartBugs安装小问题总结
- Redis learning notes
- Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
- [batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
猜你喜欢
![[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)](/img/8c/7345b4e8a55dcf11d18ac113df4fc2.png)
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)

shell小技巧(一百三十四)简单的键盘输入记录器

机器学习笔记 - 时间序列的线性回归

test

GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items

Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification

Sichuan earth microelectronics 8-channel isolated digital input receiver

Five causes of PCB board deformation and six solutions 2021-10-08

Summary of small problems in smartbugs installation

realsense d455 semantic_slam实现语义八叉树建图
随机推荐
[pytest] modify the logo and parameterization in the allure Report
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
数据可视化没有重点怎么办?
Hisilicon 3559 sample parsing: Vio
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
opencv最小值滤波(不局限于图像)
Path planner based on time potential function in dynamic environment
[batch dos-cmd command - summary and summary] - add comment command (REM or::)
【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
Ns32f103c8t6 can perfectly replace stm32f103c8t6
C reads XML on the web
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
Keil and Proteus joint commissioning
NPM install reports an error: gyp err! configure error
Shell tips (134) simple keyboard input recorder
Bicubic difference