LeetCode 27
2016-04-24 23:00
295 查看
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 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.
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.
public class Solution { public int removeElement(int[] nums, int val) { int a = 0; int b = 0; int n = 0; for(int i=0; i<nums.length; i++){ if(val == nums[i]){ a++; n++; }else{ nums[b] = nums[a]; a++; b++; } } return nums.length-n; } }
相关文章推荐
- Android-Toast的属性设置
- 22-reverseString-Leetcode
- 邮票分你一半(nyoj_456)
- 冒泡
- 【转载】装饰模式与代理模式的区别
- C使用FILE指针文件操作
- sybase15 迁移数据库时遇到的问题
- 关于int类型取值范围的计算 以及为何16位int类型范围是- 32768 ~ 32767
- Redis源码分析——SDS
- JSP网页弹窗代码详解
- [Perl] $SIG{HUP}
- php 信号量
- Tribonacci数列前n项和的求解问题
- 第一篇博客——开始记录自己的CS学习之路
- 项目与数据库有关的三种角色
- UWB在中国可使用频段的担心
- LeetCode 7
- 88. Merge Sorted Array
- java并发之Semaphore(信号量)
- html中的title和alt