当前位置:网站首页>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;}}
边栏推荐
- Getting Started with Three.JS Programmatic Modeling
- Running yum reports Error: Cannot retrieve metalink for reposit
- 【Acunetix-忘记密码】
- 力扣58-左旋转字符串
- npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
- redis cluster cluster, the ultimate solution?
- Jest 测试框架 beforeEach 的设计原理解析
- npm install报错npm ERR Could not resolve dependency npm ERR peer
- 细学常用类,集合类,IO流
- [kali-information collection] (1.8) ARP reconnaissance tool _Netdiscover
猜你喜欢
随机推荐
npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive
免费文档翻译-免费批量文档翻译软件推荐
Pytorch 占用cpu资源过多
ASP.NET Core 6框架揭秘实例演示[31]:路由&ldquo;高阶&rdquo;用法
雷克萨斯,锁死的安全,挡不住的心寒
【2022 小目标检测综述】Towards Large-Scale Small Object Detection: Survey and Benchmarks
您应该知道的 Google Sheets 使用技巧
sqli-labs(less-11)
ES2020-23简单易懂又实用的精选特性讲解 日常开发必备干货!
Golang map数组按字段分类
划分训练集,验证集,测试集
SQL 经典50题(题目+解答)(1)
使用kubesphere图形界面创建一个应用操作流程
Thymeleaf
AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表
【Acunetix-Forgot your password】
8大软件供应链攻击事件概述
Coroutines and Lifecycle in Kotlin
免费的中英文翻译软件-自动批量中英文翻译软件推荐大全
使用无界队列的线程池会导致内存飙升吗?