当前位置:网站首页>LeetCode每日一练 —— 27. 移除元素
LeetCode每日一练 —— 27. 移除元素
2022-07-26 18:33:00 【飞向星的客机】
前言
Wassup guys!我是Edison
今天是 LeetCode 上的 leetcode 27. 移除元素
Let’s get it!

1. 题目分析
数组
nums和一个值val,你需要 原地 移除所有数值等于val的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O ( 1 ) O(1) O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
示例 1:
示例 2:
2. 题目图解
思路一
开辟一个和 nums 大小一样的新数组,把不是 val 的值拷贝到新数组
首先指针 src 指向 nums 数组中的起始位置;指针 dst 指向 newNums 数组中的起始位置
此时,src指向的元素为 0,不是 2,那么就把数字 0 放到 newNums 数组中,指针 src 和 dst 同时向后挪动,如图所示
此时,src指向的元素为 1,不是 2,那么就把数字 1 放到 newNums 数组中,指针 src 和 dst 同时向后挪动,如图所示
此时,src指向的元素为 2,与 val 的值相等,那么就把指针 src 向后挪动,指针 dst 不动,如图所示
此时,src指向的元素为 2,与 val 的值相等,那么就把指针 src 向后挪动,指针 dst 不动,如图所示
此时,src指向的元素为 3,不是 2,那么就把数字 3 放到 newNums 数组中,指针 src 和 dst 同时向后挪动,如图所示
此时,src指向的元素为 0,不是 2,那么就把数字 0 放到 newNums 数组中,指针 src 和 dst 同时向后挪动,如图所示
此时,src指向的元素为 4,不是 2,那么就把数字 4 放到 newNums 数组中,指针 src 和 dst 同时向后挪动,如图所示
此时,src已经指向了最后一个元素为 2,与 val 的值相等,那么就结束循环,如图所示
此方法的时间复杂度为 O ( n ) O(n) O(n),空间复杂度也是 O ( n ) O(n) O(n);不符合题意
思路二
此方法是思路一的升级,不开辟新数组,直接在原数组使用双指针
指针 src 和 dst 同时指向 nums 的起始位置,如图所示
此时 src 指向的元素为 0,不等于 val(2),那么就用 src 指向的元素去 覆盖 掉 dst 指向的元素,然后 src 和 dst 同时向后挪动,如图所示
此时 src 指向的元素为 1,不等于 val(2),那么就用 src 指向的元素去 覆盖 掉 dst 指向的元素,然后 src 和 dst 同时向后挪动,如图所示
此时 src 指向的元素为 2,等于 val(2),那么就只把指针 src 向后挪动,指针 dst 不动,如图所示
此时 src 指向的元素还是 2,等于 val(2),依旧只把指针 src 向后挪动,指针 dst 不动,如图所示
此时 src 指向的元素为 3,不等于 val(2),那么就用 src 指向的元素去 覆盖 掉 dst 指向的元素,然后 src 和 dst 同时向后挪动,如图所示
此时 src 指向的元素为 0,不等于 val(2),那么就用 src 指向的元素去 覆盖 掉 dst 指向的元素,然后 src 和 dst 同时向后挪动,如图所示
此时 src 指向的元素为 4,不等于 val(2),那么就用 src 指向的元素去 覆盖 掉 dst 指向的元素,然后 src 和 dst 同时向后挪动,如图所示
此时 src 指向最后一个元素 2,等于 val(2),那么直接结束,返回 dst 的长度
所以 dst 就是用来删除数组中等于 val 的值,然后把不等于 val 的值依次往后放!所以这道题的结构如下
3. 代码实现
接口代码
int removeElement(int* nums, int numsSize, int val){
int src = 0;
int dst = 0;
int i = 0;
while (src < numsSize) {
if (nums[src] != val) {
nums[dst] = nums[src];
src++;
dst++;
}
else {
src++;
}
}
return dst;
}
提交结果
边栏推荐
- 2022搭建企业级数据治理体系
- 关于接口测试你想知道的都在这儿了
- 通过源码深度分析线程池中Worker线程的执行流程
- 查看容器的几种方式
- J1: why is redis so fast + basic structure
- canvas 图形
- Save 50% of the cost JD cloud releases a new generation of hybrid CDN products
- After working for 13 years, I have a little software testing experience and feelings
- Ijcai2022 meeting! Brescia et al. Tutorial of evidential reasoning and learning, describing its latest progress, with 96 slides attached
- Volatile keyword of JVM memory model
猜你喜欢

After working for 13 years, I have a little software testing experience and feelings

The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

Software process that testers must know

客户案例 | 聚焦流程体验,助银行企业APP迭代

J3: redis master-slave replication

Customer cases | focus on process experience to help bank enterprise app iteration

Leetcode-138-copy linked list with random pointer

手机app测试用例怎么写?手机app测试点有哪些?

博客维护记录之图片预览嵌入位置问题
随机推荐
EN 1504-7 products for protection and repair of concrete structures corrosion prevention of reinforcement - CE certification
"Weilai Cup" 2022 Niuke summer multi school training camp 1
The authentication type 10 is not supported
C # get local time / system time
中信建投启牛学堂开户是安全的吗,启牛是干嘛的
Several ways to view containers
数据湖--概念、特征、架构与案例概述
Configure the server environment
如何保护电子商务网站免受网络攻击?
B站SRE负责人亲述 713事故后的多活容灾建设|TakinTalks大咖分享
首席信息官引导业务变革的指南
Will 3R balanced financial products have risks? Is it risky?
Cuda11.2 corresponding pytorch installation
基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料
Leetcode-138-copy linked list with random pointer
C#创建及读取DAT文件案例
Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
Adjust the array order so that odd numbers precede even numbers and their relative positions remain the same
C#上位机开发—— 修改窗口图标和exe文件图标
J1: why is redis so fast + basic structure