leetcode-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 doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
思路就是类似26题。。
public class Solution {
public int removeElement(int[] nums, int val) {
if(nums==null || nums.length<1) return 0;
int pos = 0;
int dis = 0;
while(pos+dis<nums.length){
nums[pos] = nums[pos+dis];
if(nums[pos]!=val){
pos++;
}else{
dis++;
}
}
return pos;
}
}