leetcode解题报告(8):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 in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.
分析
遍历该vector,判断当前元素是否和给定val相等,如果相等,就将该值删除,否则就遍历下一个元素,需要注意的一点是调用erase函数会使迭代器失效。如:
vector<int>vi = {1,2,3,4,5,6};
for(auto i = vi.begin(); i != vi.end(); ++i){
if(*i == 3)
vi.erase(i); //error:iterator i is invalid
}
调用vi.erase(i),会使迭代器i失效,在这种情况下,对迭代器做任何操作都会出错。正确的方法是给定一个返回值,该返回值指向被删除的元素的下一元素:
vector<int>vi = {1,2,3,4,5,6};
for(auto i = vi.begin(); i != vi.end();){
if(*i == 3)
i = vi.erase(i); //error:iterator i is invalid
else
++i;
}
代码如下:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
auto iter = nums.begin();
while(iter != nums.end()){
if(*iter == val)iter = nums.erase(iter);
else ++iter;
}
return nums.size();
}
};
也可以不用erase函数,另一种解法如下:
https://discuss.leetcode.com/topic/20654/a-simple-c-solution
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int index = 0;
for(int i = 0; i != nums.size(); ++i){
if(nums[i] != val){
nums[index] = nums[i];
++index;
}
}
return index;
}
};
leetcode解题报告(8):Remove Element的更多相关文章
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 【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 题目整理-7 Remove Element & Implement strStr()
27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- leetcode解题报告(6):Remove Duplicates from Sorted List
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
随机推荐
- css 样式合集
td换行: style="word-wrap:break-word;word-break:break-all;" 超长省略号: table { table-layout: fixe ...
- 【计算几何】Water Testing
Water Testing 题目描述 You just bought a large piece of agricultural land, but you noticed that – accord ...
- redis键的迁移操作
1.redis单个实例内多库间的数据迁移操作 命令名称:move 语法:move key db 功能:将当前数据库的key移动到给定的数据库db当中.如果当前数据库(源数据库)和给定数据库(目标数据库 ...
- (六)在线文档编辑器的使用和数据字典(ueditor编辑器/my97datepicker日期控件)
使用ueditor编辑器注意: 1. 要把ueditor的jar包添加到WEB-INF/lib里. 2. 在做图片上传等功能时,必须重写struts的过滤器,否则图片流会被拦截程序无法得到图片. 3. ...
- SqlServer用sql语句清理log日志
原文:SqlServer用sql语句清理log日志 USE[master] ALTER DATABASE [Center] SET RECOVERY SIMPLE WITH NO_WAIT ALTER ...
- mysql 2 修改表
1 修改表名 rename table aaa to bbb; 或者 rename table aaa to bbb; 2 修改字段的数据类型 alter table person modify na ...
- c#基础知识梳理(二)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10818256.html 一.变量 一个变量只不过是一个供程序操作的存储区的名字.在 C# 中,每个变量都有一 ...
- SublimeText 3 常见快捷键
·F12 跳转至预定义 ·F11 全屏模式 ·Ctrl+A 本文全选 ·Ctrl+F 打开底部搜索框,查找关键字. ·Ctrl+D 本文件选中光标选中的单词 ·Ctrl+L 选中光标所在一行 ·C ...
- 批量删除checkbox前台后台
<%@ page contentType="text/html;charset=UTF-8" %><%@ include file="/WEB-INF/ ...
- JS密码强度检测
//校验密码强度---沒有匹配到以下級別就提示 function checkPassWord(value){ // 0: 表示第一个级别 1:表示第二个级别 2:表示第三个级别 // 3: 表示第四个 ...