当前位置:网站首页>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;}}
边栏推荐
猜你喜欢
随机推荐
The exchange - string dp
MP的几种查询方式
SQL 数据更新
喜迎八一 《社会企业开展应聘文职人员培训规范》团体标准出版发行会暨橄榄枝大课堂上线发布会在北京举行
DTG-SSOD:最新半监督检测框架,Dense Teacher(附论文下载)
Kotlin的协程与生命周期
图形处理单元(GPU)的演进
JSP中include指令的功能简介说明
[kali-information collection] (1.8) ARP reconnaissance tool _Netdiscover
Pytorch 占用cpu资源过多
jacoco的学习以及理解
翁恺C语言程序设计网课笔记合集
Getting Started with Three.JS Programmatic Modeling
记录代码
受邀出席Rust开发者大会|Rust如何助力量化高频交易?
力扣35-搜索插入位置——二分查找
QAbstractScrollArea、QScrollArea
SQLAlchemy使用教程
Create a devops CI/CD process using the kubesphere GUI
SQL(面试实战07)