27. Remove Element
2016-03-18 21:07
288 查看
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 =
Your function should return length = 2, with the first two elements of nums being 2.
Show Hint
Subscribe to see which companies asked this question
快慢指针
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.
Show Hint
Subscribe to see which companies asked this question
快慢指针
public class Solution { public int removeElement(int[] nums, int val) { int p1 = 0 ; int p2 = 0 ; int len = nums.length; for(int i = 0;i<len;i++){ if(nums[i]!=val){ p1++; nums[p1-1]=nums[i]; } }return p1; } }
相关文章推荐
- Combination Sum
- 3192: [JLOI2013]删除物品
- iOS第三方库AFNetworking、SDWebImage
- 细谈select函数(C语言)
- 算日期模板
- 《不要因为走得太远 而忘记为什么出发》
- 基础测试查漏补缺
- 【bzoj 3192】: [JLOI2013]删除物品 树状数组+想法题
- POJ 2773 Happy 2006#素数筛选+容斥原理+二分
- 使用squid实现普通的代理服务器
- oracle允许外网链接
- 107. Binary Tree Level Order Traversal II
- nyoj--301 递推求值(矩阵快速幂)
- Pairs Forming LCM 素数筛和素因子分解
- 0-1背包问题
- 最大子段和
- [Angular 2] Using events and refs
- 如何在jsp页面中利用el表达式的<p>${error }</p>取值?
- 198. House Robber
- 指针与引用的区别