当前位置:网站首页>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 ~
边栏推荐
- VSCode很好,但我以后不会再用了
- Runtime - Methods member variable, cache member variable
- Invalid Navicat scheduled task
- Fairmot yolov5s to onnx
- 【视频】ffplay 使用mjpeg格式播放usb摄像头
- 三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
- Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
- useMemo模拟useCallback
- Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
- 双三次差值bicubic
猜你喜欢

Leetcode daily question - 515 Find the maximum value in each tree row

Understand the reasons for impedance matching of PCB circuit board 2021-10-07

"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud

How much do you know about electronic components on PCB?

What are the problems with traditional IO? Why is zero copy introduced?

ts环境搭建

VOCALOID笔记

Share the process requirements for single-layer flexible circuit board

基于Anaconda的模块安装与注意事项

Misunderstanding of switching triode
随机推荐
Vscode is good, but I won't use it again
C get the version number of exe - file version and assembly version
Static bit rate (CBR) and dynamic bit rate (VBR)
年后求职找B端产品经理?差点把自己坑惨了......
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
“空间转换”显著提升陡崖点云的地面点提取质量
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
Runtime - Methods member variable, cache member variable
个人域名和企业域名的区别
搞清信息化是什么,让企业转型升级走上正确的道路
opencv最小值滤波(不局限于图像)
Atlassian confluence漏洞分析合集
useMemo模拟useCallback
Tips 𞓜 how to clean PCB boards 2021-10-22
OAuth 2.0 one click login
【视频】ffplay 使用mjpeg格式播放usb摄像头
C reads XML on the web
Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134漏洞分析与防护
Requirements for Power PCB circuit board design 2021-11-09
Keil and Proteus joint commissioning