当前位置:网站首页>【C语言】LeetCode27.移除元素
【C语言】LeetCode27.移除元素
2022-07-31 15:53:00 【阿亮joy.】
移除元素
描述:
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
说明:为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
int len = removeElement(nums, val);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[i]);
}
示例 1
输入:nums = [3,2,2,3], val = 3输出:
2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
示例 2
输入:nums = [0,1,2,2,3,0,4,2], val = 2输出:
5, nums = [0,1,4,0,3]解释:
函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
前情提醒: 对于本道题目,博主将给出三种解题的思路,三种思路由劣到优。因为我们都不可能一下子就想到最优的算法,这是需要慢慢练习的,话不多说直接开整。
思路一
找到所有的val,依次向前挪动数据覆盖删除。

代码示例:
#include <stdio.h>
int removeElement3(int* nums, int numsSize, int val)
{
int begin = 0;
int n = numsSize;
while (begin < n)
{
if (nums[begin] == val)
{
for (int i = begin + 1; i < n; i++)
{
nums[i - 1] = nums[i];
}
n--;//有元素的值为val,往前覆盖,元素个数减一
}
else
{
begin++;
}
}
return n;
}
int main()
{
int arr[6] = { 1,3,3,4,3,5 };
int len = removeElement3(arr, 6, 3);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
return 0;
}输出结果:

分析:时间复杂度为O(N^2),最坏情况:数组中大部分值甚至全部都是val,空间复杂度为O(1)。该方法能够通过LeetCode的测试,但是时间复杂度过高,不是很好的方法。
思路二
用空间换取时间,一次遍历数组nums,把不是val的值,放到数组temp,再把数组temp的值拷贝回去。

代码示例:
#include <stdio.h>
#include <string.h>
int removeElement2(int* nums, int numsSize, int val)
{
int temp[numsSize] = {0};
int i = 0;
int count = 0;
for (i = 0; i < numsSize; i++)
{
if (nums[i] != val)
{
temp[count] = nums[i];
count++;
}
}
memcpy(nums, temp, sizeof(int) * count);
return count;
}
int main()
{
int arr[6] = { 1,3,3,4,3,5 };
int len = removeElement2(arr, 6, 3);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
return 0;
}输出结果:

分析:时间复杂度为O(N),空间复杂度为O(N),不满足题目要求的空间复杂度为O(1)。
思路三
利用双指针的思想,定义两个整型变量src和dst,src去找nums数组中不等于val的值,放到dst指定的位置去,在src++,dst++。

代码示例:
#include <stdio.h>
int removeElement1(int* nums, int numsSize, int val)
{
int src = 0;
int dst = 0;
while (src < numsSize)
{
if (nums[src] != val)
{
nums[dst] = nums[src];
src++;
dst++;
}
else
{
src++;
}
}
return dst;
}
int main()
{
int arr[6] = { 1,3,3,4,3,5 };
int len = removeElement1(arr, 6, 3);
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
输出结果:

分析:时间复杂度为O(N),空间复杂度为O(1),该方法已经是这道题目的最优解了。可以学习一下这个思路。
以上就是本篇博客的全部内容了,如果大家觉得有收获的话,可以点个三连支持一下!谢谢大家啦!
结语
每个优秀的人都有一段沉默的时光,那段时光是付出了很多努力却得不到结果的日子,我们把它叫做扎根。
边栏推荐
- After Grafana is installed, the web opens and reports an error
- Getting Started with TextBlock Control Basic Tools Usage, Get Started
- 多主复制下处理写冲突(4)-多主复制拓扑
- Doing things software development - the importance of law and understanding of reasonable conclusions
- org.apache.jasperException(could not initialize class org)
- [MySQL] Mysql paradigm and the role of foreign keys
- 【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
- Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
- Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
- Visualize GraphQL schemas with GraphiQL
猜你喜欢
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)
[TypeScript] In-depth study of TypeScript type operations

长得很怪的箱图

Why don't you make a confession during the graduation season?

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

C语言”三子棋“升级版(模式选择+AI下棋)

border控件的使用

浏览器自带的拾色器

Foreign media right, apple on May be true in inventory

Qt实战案例(54)——利用QPixmap设计图片透明度

T - sne + data visualization parts of the network parameters
随机推荐
Baidu cloud web speed playback (is there any website available)
国内市场上的BI软件,到底有啥区别
数据表插入数据insert into
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
t-sne 数据可视化网络中的部分参数+
Browser's built-in color picker
How to switch remote server in gerrit
6-22漏洞利用-postgresql数据库密码破解
ASP.NET Core 产生连续 Guid
Why is the field of hacking almost filled with boys?
Kubernetes common commands
Emmet syntax
字符指针赋值[通俗易懂]
Doing things software development - the importance of law and understanding of reasonable conclusions
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
The arm button controls the flashing of the led light (embedded button experiment report)
Visualize GraphQL schemas with GraphiQL
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
复制延迟案例(1)-最终一致性
SringMVC中个常见的几个问题