LeetCode 27. Remove Elemen
2016-06-03 15:51
337 查看
Remove Elemen
Des
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 = 3
Your function should return length = 2, with the first two elements of nums being 2.
分析
这个题很简单,有个注意:如果使用erase(itr)函数的话,其删除之后要返回该元素后边的一个元素Code
class Solution { public: int removeElement(vector<int>& nums, int val) { for(vector<int>::iterator it=nums.begin();it!=nums.end();){ if(*it==val){ it=nums.erase(it); }else{ it++; } } return nums.size(); } };
相关文章推荐
- Qt 程序打包发布
- 仿照linux下的uboot来DIY自己的arm11(6410)的bootloader
- 配置高可用的Hadoop平台
- 如何让linux时间与internet时间同步(centos)
- 也谈谈同源策略和跨域问题
- [正则表达式]PCRE反向分组引用
- centos 发送邮件
- Android设置状态栏透明代码!!!!
- 和Linux大魔王一起愉快的玩耍(二)make命令和mekefile
- hibernate中的addEntity setResultTransformer的比较
- 二进制位翻转:
- System.arraycopy 实现多次点击
- Android_异步加载2
- redis3.0配置调优
- 使用orm(Sequelize) 操作 MySQL (2)
- Android长按Power键弹出关机Dialog框GlobalActions解析
- 实验二作业调度模拟程序
- exit--进程退出;wait--进程等待;execl--执行程序
- mysql备份还原
- mysql遇到锁表常用命令