当前位置:网站首页>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;}}
边栏推荐
- SQL函数 $TRANSLATE
- 观察者(observer)模式(二) —— 实现线程安全的监听器
- The exchange - string dp
- C#为listview选中的项添加右键菜单
- Mysql事务隔离级别与MVCC(多版本并发控制)
- Create a devops CI/CD process using the kubesphere GUI
- 划分训练集,验证集,测试集
- npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive
- 半夜赶工制作简报的我好想说 : 确定了,最终稿就是这样
- Idea 全局搜索(idea如何全局搜索关键字)
猜你喜欢
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
CAN总线的AUTOSAR网络管理
基于深度学习的裂缝检测技术
力扣151-颠倒字符串中的单词
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
[kali-information collection] (1.9) Metasploit + search engine tool Shodan
Mysql transaction isolation level and MVCC (multi-version concurrency control)
Getting Started with Three.JS Programmatic Modeling
Shell编程案例
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
随机推荐
ansible模块--yum模块
匹配滤波(四种滤波器的幅频特性)
Failure Analysis | A SELECT statement crashes MySQL, what happened?
JVM简介
SQL function $TRANSLATE
Excel动态图制作
学习经验分享之七:YOLOv5代码中文注释
力扣704-二分查找
免费文档翻译-免费批量文档翻译软件推荐
注意力机制
[kali-information collection] (1.8) ARP reconnaissance tool _Netdiscover
jacoco的学习以及理解
MySQL主从复制几个重要的启动选项
ssm web page access database data error
leetcode: 200. Number of islands
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
【云驻共创】数据工坊平台,0代码开发数据处理业务“快”人一步
数字化转型中的低代码
SQL 数据更新
细学常用类,集合类,IO流