Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
这题一开始以为很简单,没想到这个坐标把我纠结了半天,不停的调试,快submit了几十次了= =
public class Solution {
public int removeElement(int[] nums, int val) {
int offset=0;
int i=0;
int pos=nums.length;
if(pos-1==i) return nums[0]==val?0:1;
while(i<nums.length && pos>i){
if(nums[i]==val){
offset++;
while(pos-1>i && nums[pos-1]==val){
pos--;
offset++;
}
if(pos==0) return 0;
if(pos<=i) break;
nums[i]=nums[pos-1];
nums[pos-1]=val;
pos--;
}
i++;
}
return nums.length-offset;
}
}
以及晒一下图片 自己都被感动到了!!