leetcode刷题日记——Remove Element
2015-12-18 13:34
232 查看
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
问题分析:题目目标就是删除掉目标元素。解决办法,将向量中的不是目标元素重组,返回长度即可;实现代码如下:
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
问题分析:题目目标就是删除掉目标元素。解决办法,将向量中的不是目标元素重组,返回长度即可;实现代码如下:
class Solution { public: int removeElement(vector<int>& nums, int val) { if(nums.size()==0) return 0; int size=nums.size(); int index=0; for(int i=0;i<size;i++){ if(nums[i]!=val) nums[index++]=nums[i]; } return index; } };
相关文章推荐
- mime类型表
- hdu1421 搬寝室
- 砍树问题
- HDOJ 1232 畅通工程
- 2015年下半年工作总结。
- 列表选择框spinner
- 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】下 ~ Net程序员的福利
- wordpress优化:Gravatar头像被墙及解决方案
- 手机号码归属地api文档
- Oracle 自己主动内存參数依赖性
- 世界工资研究报告与借鉴
- 构建Unity3D信号槽机制 【ZObject.cs】
- Python面向对象
- HDU2050折线分割平面
- js数组引用传递
- HDOJ 2545 树上战争
- Mac OS X 10.10 启用 Web 服务器
- 【鬼脸原创】github搭建动态网站
- volatile
- wpa_cli