leetcode 027 —— Remove Element
2015-07-13 20:23
281 查看
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) {
vector<int>::iterator i = nums.begin();
while (i != nums.end()){
if (*i == val)
i = nums.erase(i);
else
i++;
}
return nums.size();
}
};
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) {
vector<int>::iterator i = nums.begin();
while (i != nums.end()){
if (*i == val)
i = nums.erase(i);
else
i++;
}
return nums.size();
}
};
相关文章推荐
- Node:普通归并
- 英格兰、威尔士、北爱尔兰教育体系
- 使用const关键字进行函数重载
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
- 第一篇博客
- POJ 2481 Cows
- Gym 100496A Avangard Latin Squares(矩阵游戏)
- JSP 标准标签库(JSTL)之最常用的JSTL标签总结
- const型变量与函数重载
- CXF WebService整合Spring
- 算法导论 第五章:随机算法
- 类的const和非const成员函数的重载
- hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理
- C++ 学习之函数重载、基于const的重载
- javaWeb
- 跟着实例学习设计模式(8)-适配器模式adapter(结构型)
- Linux下使用Caffe对图片进行训练并分类的简单流程
- [C++] const与重载
- CPU上涨100%的问题排查
- php中文乱码问题分析及解决办法