算法题丨Remove Element
描述
Given an array and a value, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
示例
Given nums = [3,2,2,3], val = 3, Your function should return length = 2,
with the first two elements of nums being 2.
算法分析
难度:低
分析:给定数组和指定一个目标值,从数组中移除所有跟目标值相等的元素,返回最终元素的长度,注意不要另外分配内存空间。
思路:题目很简单,直接遍历数组元素,判断当前元素是否跟目标值相等,如果不相等,证明当前元素应该留在数组中,有效数组长度自增1,否则为无效元素,因为只需返回有效数组长度,所以不用删除元素,跳过此循环即可。
代码示例(C#)
public int RemoveElement(int[] nums, int val)
{
int i = 0;
for (int j = 0; j < nums.Length; j++)
{
//如果不相等,有效长度自增1
if (nums[j] != val)
{
nums[i] = nums[j];
i++;
}
}
return i;
}
复杂度
- 时间复杂度:O (n).
- 空间复杂度:O (1).
附录
- 系列目录索引
- 代码实现(C#版)
- 相关算法:Move Zeroes
算法题丨Remove Element的更多相关文章
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- LeetCode算法题-Kth Largest Element in a Stream(Java实现)
这是悦乐书的第296次更新,第315篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第164题(顺位题号是703).设计一个类来查找流中第k个最大元素.请注意,它是排序顺序 ...
- LeetCode算法题-Next Greater Element I(Java实现)
这是悦乐书的第244次更新,第257篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第111题(顺位题号是496).你有两个数组(没有重复)nums1和nums2,其中nu ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- 算法题丨Two Sum
描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- AJAX跨域问题解决方法(3)——被调用方解决跨域
被调用方解决跨域是指在HTTP响应头中增加指定的字段,允许调用方调用 可以在两种地方增加1.apache/nginx(HTTP服务器)2.tomcat(应用服务器) 浏览器如何判断跨域?仔细观察可以发 ...
- 炫丽的倒计时效果Canvas绘图与动画基础
前言 想要在自己做的网页中,加入canvas动画效果,但是发现模板各种调整不好,觉得还是要对canvas有所了解,才可以让自己的网页变得狂拽炫酷吊炸天! 一.绘制基础 1 <!DOCTYPE h ...
- vc的环境变量配置和缺少mspdb60.dll的解决方法
vc的编译器是cl.exe,我们如果在vc中编译就不用配置环境,但是如果要在任何位置用命令提示符打开编译器cl.exe来编译程序,那么就要配置环境了. 下面我就讲讲vc的环境变量配置和缺少mspdb6 ...
- Canvas 画布组件(官网翻译)
Canvas画布 The Canvas is the area that all UI elements should be inside. The Canvas is a Game Object w ...
- linux编程基础汇总贴
linux编程基础汇总贴http://newzol.cn/forum.php?mod=viewthread&tid=67&fromuid=3(出处: newzol) 1.管道 http ...
- 关于Cesium中的常用坐标系及说明
Cesium是一个基于JavaScript的开源框架,可用于在浏览器中绘制3D的地球,并在其上绘制地图(支持多种格式的瓦片服务),该框架不需要任何插件支持,但是浏览器必须支持WebGL. Cesium ...
- go语言defer panic recover用法总结
defer defer是go提供的一种资源处理的方式.defer的用法遵循3个原则 在defer表达式被运算的同时,defer函数的参数也会被运算.如下defer的表达式println运算的同时,其入 ...
- mybatis返回list
1 Model类 public class Vo { /** * this is used for receive data partly from table user_question_secti ...
- 获取DOM节点的几种方式
DOM 是一个树形结构,操作一个DOM节点,实际上就是这几个操作:更新.删除.添加.遍历 在操作DOM节点之前,需要通过各种方式先拿到这个DOM节点,常用的方法有: 一.通过元素类型的方法来操作: d ...
- centOS7安装nodejs(8.4.0)(详细步骤)
1.使用rpm查看是否安装gcc.make 若如下图有输出版本详细表示已安装,则无需再次安装,直接下一步(输入rpm -qa 包名称) 若没有安装则执行以下命令安装: yum install gcc ...