[leetcode] 【数组】27. Remove Element
2016-05-22 03:01
459 查看
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.
和去重复的题类似,两个操作:1计数、2元素移动。
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.
题解
除去数组中的目标元素,返回剩余元素个数m,并且数组前m个元素为除去目标元素后的数组元素。和去重复的题类似,两个操作:1计数、2元素移动。
//cpp class Solution { public: int removeElement(vector<int>& nums, int val) { int index=0; for(int i=0;i!=nums.size();i++) { if(nums[i]!=val) nums[index++]=nums[i]; } return index; } };
相关文章推荐
- 明明将gson-2.3.1.jar 包复制粘贴在所在工程libs目录下,却无法使用
- 将项目改为MRC的方法(有图有真相)
- Window7下安装openssl完整版(亲测实现)
- kubernates
- Minimum Path Sum
- 大数据学习笔记·城市计算(3)
- [leetcode] 【数组】18. 4Sum
- 自定义控件
- 关于js传送json到.net后台处理
- grunt的安装使用
- 微信开发获取code
- tracker-store and tracker-miner-fs eating up my CPU on every startup
- [leetcode] 【数组】16. 3Sum Closest
- tracker-store and tracker-miner-fs eating up my CPU on every startup
- Ubuntu(Linux) 下 unzip 命令使用详解
- [leetcode] 【数组】15. 3Sum
- C# 文件的保存与读取
- HTML5的一些术语和概念
- python with
- cp: cannot create symbolic link XXX Operation not permitted