[LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, 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.
Example 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5
, with the first five elements ofnums
containing0
,1
,3
,0
, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
这道题让我们移除一个数组中和给定值相同的数字,并返回新的数组的长度。是一道比较容易的题,只需要一个变量用来计数,然后遍历原数组,如果当前的值和给定值不同,就把当前值覆盖计数变量的位置,并将计数变量加1。代码如下:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int res = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != val) nums[res++] = nums[i];
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/27
类似题目:
Remove Duplicates from Sorted Array
参考资料:
https://leetcode.com/problems/remove-element/
https://leetcode.com/problems/remove-element/discuss/12286/Accepted-java-solution
https://leetcode.com/problems/remove-element/discuss/12289/My-solution-for-your-reference.
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 27. Remove Element 移除元素的更多相关文章
- [LeetCode]27. Remove Element移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 27. Remove Element - 移除元素-Easy
Description: Given an array and a value, remove all instances of that value in place and return the ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [leetcode]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 027 Remove Element 移除元素
给定一个数组和一个值,在这个数组中原地移除指定值和返回移除后新的数组长度.不要为其他数组分配额外空间,你必须使用 O(1) 的额外内存原地修改这个输入数组.元素的顺序可以改变.超过返回的新的数组长度以 ...
- Leetcode 27——Remove Element
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
随机推荐
- redis之布隆过滤器
布隆过滤器是什么? 布隆过滤器可以理解为一个不怎么精确的 set 结构,当你使用它的 contains 方法判断某个对象是否存在时,它可能会误判.但是布隆过滤器也不是特别不精确,只要参数设置的合理,它 ...
- 【maven】【IDEA】idea中使用maven编译项目,报错java: 错误: 找不到符号 【2】
=================================================================================== idea中使用maven编译项目 ...
- 使用IDEA的Git插件上传项目教程
如何使用IDEA的Git插件上传项目 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载 二.注册码云账号 搜索gitee码云插件并安装
- 百度编辑器上传视频报Http请求错误,.net实现方式
在使用百度编辑器上传视频的时候遇到一个很奇怪的问题,当视频大小在20M以下的时候,上传正常.当大于20M时,一直报Http请求错误. 处理步骤: 1.修改编辑器配置信息,如图所示,改成你想要的大小 2 ...
- Python如何实现单例模式?其他23中设计模式python如何实现?
单例模式主要有四种方法:new.共享属性.装饰器.import. # __ new__方法: class Singleton(object): def __new__(cls, *args, **kw ...
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
转自:http://blog.csdn.net/java886o/article/details/9041547 FileTools.h FileTools.m #import "FileT ...
- 不了解MES系统中的看板管理?看完本文就懂了
如果想要在生产车间中,让生产过程管理都处在“看得见”的状态,那么看板可视化管理的导入是你的不二选择. MES看板包括四个部分:生产任务看板.各生产单位生产情况看板.质量看板和物料看板,其中生产任务看板 ...
- maven 学习---Eclipse构建Maven项目
1. 安装m2eclipse插件 要用Eclipse构建Maven项目,我们需要先安装meeclipse插件 点击eclipse菜单栏Help->Eclipse Marketplac ...
- maven 学习---Maven添加远程仓库
默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...
- Delphi-基础(for循环)
1.判断0~10之间,当循环I=3时候跳出当前循环,当I等于8时候,退出当前循环. procedure Countand(); var I: Integer; begin do begin Write ...