LeetCode 27. Remove Element
2016-07-22 13:34
344 查看
27. Remove Element
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 does not 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.
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 does not 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.
class Solution { public: int removeElement(vector<int>& nums, int val) { int len = 0; for(int i = 0; i < nums.size(); i++) { if(nums[i] != val) { nums[len++] = nums[i]; } } return len; } };
相关文章推荐
- 首页 - 纳人
- 2016暑假多校联赛 HDU5738
- oracle创建表空间,用户,授权
- Java学习——HashSet
- 快速搞懂 ”ld“ ——链接器
- LeetCode 231. Power of Two
- swift - UIAlertController 的用法
- 对数线性模型之一(逻辑回归), 广义线性模型学习总结
- JavaScript多选框的全选与取消(未测试)
- sqlite时间函数及时间处理
- java 几种排序
- JavaScript本地对象 内置对象 宿主对象
- JavaScript实现图片轮播(未测试)
- Windows下虚拟机安装Mac OS X —– VM12安装Mac OS X 10.11
- Xcode7中创建静态库
- Android5.0,状态栏颜色任我改
- 如何利用python+mySQL写个数据库并且利用python将所需变量值写入数据库
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- java常用方法
- js正则表达式:验证邮箱格式、密码复杂度、手机号码、QQ号码(未测试)