当前位置:网站首页>27. remove elements
27. remove elements
2022-06-25 07:50:00 【AlbertOS】
introduce
Give you an array n u m s nums nums And a value v a l val val, You need In situ Remove all values equal to v a l val val The elements of , And return the new length of the array after removal .
Don't use extra array space , You have to use only O ( 1 ) O(1) O(1) Additional space and In situ Modify input array .
The order of elements can be changed . You don't need to think about the elements in the array that follow the new length .
Example
Input :nums = [3,2,2,3], val = 3
Output :2, nums = [2,2]
explain : Function should return the new length 2, also nums The first two elements in are 2. You don't need to think about the elements in the array that follow the new length . for example , The new length returned by the function is 2 , and nums = [2,2,3,3] or nums = [2,2,0,0], It will also be seen as the right answer .
Input :nums = [0,1,2,2,3,0,4,2], val = 2
Output :5, nums = [0,1,4,0,3]
explain : Function should return the new length 5, also nums The first five elements in are 0, 1, 3, 0, 4. Note that these five elements can be in any order . You don't need to think about the elements in the array that follow the new length .
Answer key
Just use the double pointer to cover the position of the element you want to delete , At last, just modify the length of the array , Here I use containers vector Store array , It can intelligently adjust the array length , It is more convenient than self adjustment ~
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;
}
};
summary
Actually, I did a difficult problem today , But there is still room for improvement , I won't send that ~
边栏推荐
- VectorDraw Developer Framework 10.10
- 判断用户是否是第一次进入某个页面
- Tips on how to design soft and hard composite boards ~ 22021/11/22
- Different paths ii[dynamic planning improvement for DFS]
- [batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
- Terms and concepts related to authority and authentication system
- Six causes of PCB disconnection 2021-10-20
- Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
- Pcb|about FPC reinforcement type
- 微信小程序开通客服消息功能开发
猜你喜欢
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
Fairmot yolov5s to onnx
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
海思3559 sample解析:vio
Invalid Navicat scheduled task
How to use ad wiring for PCB design?
Five causes of PCB board deformation and six solutions 2021-10-08
消息中间件之ActiveMQ的基本使用
El input to add words to the tail
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
随机推荐
一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
Bicubic difference
VOCALOID笔记
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
OpenMP入门
Share the process requirements for single-layer flexible circuit board
如何用svn新建属于自己的分支
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
Runtime——methods成员变量,cache成员变量
C# 读取web上的xml
海思3559 sample解析:vio
How much do you know about electronic components on PCB?
Kinsing双平台挖矿家族病毒分析
基于Anaconda的模块安装与注意事项
Hisilicon 3559 sample parsing: Vio
Requirements for Power PCB circuit board design 2021-11-09
This article uses pytorch to build Gan model!
“空间转换”显著提升陡崖点云的地面点提取质量
How to use ad wiring for PCB design?
navicat定时任务无效