当前位置:网站首页>Lexicon 27 - Remove Elements - Simple Questions
Lexicon 27 - Remove Elements - Simple Questions
2022-08-02 11:46:00 【Zhang Ran Ran √】
Title description
Given an array nums and a value val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array.
Don't use extra array space, you must use only O(1) extra space and modify the input array in place.
The order of theelements can be changed.You don't need to consider elements in the array beyond the new length.
Description:
Why is the returned value an integer, but the output answer is an array?
Please note that the input array is passed "by reference", which means that modifications to the input array within the function are visible to the caller.
Solution ideas
The original intention of this question is to return an array that excludes val. The main idea is as follows:
- traverse the array nums;
- If the element is not the same as val, it does not need to be processed, if it is the same as val, then nums[i] needs to be removed;
- Returns the maximum number of indices for the last nums.
Input and output example

Code
class Solution {public int removeElement(int[] nums, int val) {int len = nums.length;int num = 0;for(int i = 0; i < len; i++){if(nums[i] != val){nums[num] = nums[i];num++;}}return num;}}边栏推荐
猜你喜欢
随机推荐
学习经验分享之七:YOLOv5代码中文注释
Excel动态图制作
Oracle 19c 连接PDB
When not to use () instead of Void in Swift
WPF 实现窗体抖动效果
翻译英语的软件-免费翻译软件-各种语言互相翻译
npm run serve启动报错npm ERR Missing script “serve“
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
DTG-SSOD:最新半监督检测框架,Dense Teacher(附论文下载)
Crack detection technology based on deep learning
力扣704-二分查找
力扣35-搜索插入位置——二分查找
面积曲线AUC(area under curve)
“纯C”实现——三子棋小游戏
QListView的使用
JSP中如何正确的填写include指令中的file路径呢?
图形处理单元(GPU)的演进
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
yolo格式(txt)数据集转VOC(xml)
Create an application operation process using the kubesphere GUI








