[LeetCode] 27 - Remove Element
2015-08-28 11:11
274 查看
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.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int num = 0;
for (int i = 0 ; i < nums.size(); ++i) {
if (nums[i] != val) {
nums[num++] = nums[i];
}
}
return num;
}
};
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) {
int num = 0;
for (int i = 0 ; i < nums.size(); ++i) {
if (nums[i] != val) {
nums[num++] = nums[i];
}
}
return num;
}
};
相关文章推荐
- 值班问题:insert语句插入了两条数据?
- Ubuntu14.04安装emacs24.5
- 老清华的闺房
- springmvc常用注解以及参数传递
- RAID5和RAID10,哪种RAID更适合你(上)
- C#中方法的参数有四种类型
- 欢迎使用CSDN-markdown编辑器
- 获取当前是今年的第几周
- Python装饰器与面向切面编程
- iOS学习 - scrollView(时钟)
- MongoDB用$group实现组合项去重
- android 学习笔记 webView的简单调用
- iScroll左右滑动阻止内容上下滑动 e.preventDefault() 解决办法
- 添加reveal库后编译错误
- c语言中常量的定义(备忘录)
- android 按钮的文字显示不全
- noip2002产生数 (高精*单精+深搜)
- ListVIew点击事件失效(item中包含checkbox)
- 图像分割之(五)活动轮廓模型之Snake模型简介
- 文章标题