[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 ...
随机推荐
- 第八周论文学习03 An Efficient Tree-based Power Saving Scheme for Wireless Sensor Networks with Mobile Sink
来源:IEEE Sensors Journal Year: 2016, Volume: 16, Issue: 20 Pages: 7545 - 7557, DOI: 10.1109/JSEN.2016 ...
- MyBatis-Generator 用法介绍
”工欲善其事,必先利其器“,古人说的很对,虽然不能做一个单纯的”工具帝“,但是自己有合适的工具集真的很关键.以前认识一个做逆向工程的高手,有自己的”反马套装“,其实不外乎就是 OD . IDA .Sy ...
- python正则图片爬取
# conding:utf8 import requests import re import time if __name__ == "__main__": # 所有的数据 ur ...
- 阿里云 CDN 业务基于边缘容器的云原生转型实践
导读:本文基于边缘容器的阿里云 CDN 云原生实践, 涵盖了边缘容器的背景和趋势,边缘托管集群 ACK Managed Edge K8s(文中简称“Edge@ACK”) 的能力.架构,以及基于边缘容器 ...
- 异步IO/协程/数据库/队列/缓存(转)
原文:Python之路,Day9 - 异步IO\数据库\队列\缓存 作者:金角大王Alex add by zhj: 文章很长 引子 到目前为止,我们已经学了网络并发编程的2个套路, 多进程,多线程,这 ...
- IDEA创建xml文件
今天在用IDEA写项目的时候发现,创建xml文件只能通过File手动输入去创建,但在我看的一个学习视频上可以直接创建xml文件,好奇之下研究了一下,作此篇,希望能对需要的朋友有所帮助. 废话就不多说了 ...
- laravel 广播细节讲解
1.应用场景 1.通知(Notification) 或 信号(Signal) 2.通知是最简单的示例,也最经常用到.信号也可看作是通知的一种展现形式,只不过信号没有UI而已. 3.Activity S ...
- JavaScript的闭包特性如何给循环中的对象添加事件(一)
初学者经常碰到的,即获取HTML元素集合,循环给元素添加事件.在事件响应函数中(event handler)获取对应的索引.但每次获取的都是最后一次循环的索引.原因是初学者并未理解JavaScript ...
- maven 学习---部署基于Maven的war文件到Tomcat
在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...
- 如何使用 Set 来提高JS代码的性能
摘要: 高效使用Set! 作者:前端小智 原文:如何使用 Set 来提高代码的性能 Fundebug经授权转载,版权归原作者所有. 为了保证的可读性,本文采用意译而非直译. 我确信有很多开发人员坚持使 ...