【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列
(一)题目
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中删除指定的元素,用到erase()函数,注意迭代器会失效
erase()会返回被删除元素的下一个元素的迭代器
*/
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
for(auto iter = nums.begin();iter!=nums.end();){
if(*iter == val){
iter = nums.erase(iter);//iter指向被删除元素的下一个元素
}
else
++iter;
}
return nums.size();
}
};
【一天一道LeetCode】#27. Remove Element的更多相关文章
- 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 nums and a value val, remove all instances of that value in-place and return the new ...
- 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 ☆
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 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
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Leetcode 27. Remove Element(too easy)
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
随机推荐
- 百度地图JS 搜索悬浮窗功能
这个需求的效果类似下面的截图,主要还是利用百度地图中自定义控件的功能,挺简单的.文档地址在这 http://lbsyun.baidu.com/index.php?title=jspopular 效果图 ...
- Lucene 查询(Query)子类
QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = ne ...
- Objective-C与Java类的一些区别
Objective-C与Java类的一些区别 OC类和C一样,需要有声明和定义,先上一段OC代码 #import <Foundation/Foundation.h> /* * 声明一个Pe ...
- Objective-C语法概述
Objective-C语法概述 简称OC 面向对象的C语言 完全兼容C语言 可以在OC里面混入C/C++代码 可以开发IOS和Mac OS X平台应用 语法预览 关键字 基本上都是以@开头(为了与C语 ...
- win2008r2 AD用户账户的批量导入方法
win2008r2 AD用户账户的批量导入方法 http://www.jb51.net/article/38423.htm
- ELK搭建
ELK安装 elasticsearch安装 * 下载elasticsearch-5.0.0.tar.gz,并解压. 通过elasticsearch.yml可设置host和port. vim confi ...
- SuperVideo,一款直播,点播,投屏并有的app
应用名称:SuperVideo应用简介: 1.聚合海量视频,视频源来源于搜狐,乐视,优酷, 腾讯等主流视频网站的丰富视频内容,最新院线大片,热播剧随时看 2.基于百度云解码,享受云解码支持RMVB,M ...
- FORM调用FORM(标准调客户化&客户化调标准)并执行查询的实现研究
一.先来个比较简单的,标准FORM调用客户话FORM并执行查询 1.修改CUSTOM.PLL,使用 fnd_function.execute实现打开和传递参数 参考例子如下 PROCEDURE eve ...
- 【SSH系列】-- hibernate基本原理&&入门demo
什么是hibernate? hibernate在英文中是冬眠的意思,当冬季来临,世界万物开始准备冬眠,但是程序猿这种动物好像不冬眠,因为需求变了,要改bug,冬眠,对于对象来说就是持 ...
- UIScrollView实现图片轮播器的无限滚动
简介 在现在的一些App中常常见到图片轮播器,一般用于展示广告.新闻等数据,在iOS内并没有现成的控件直接实现这种功能,但是通过UIScrollView的允许分页设置,可以实现滚动轮播的功能. 轮播原 ...