题目:
分析:
对于这道题,我们要做的就是如何判断其中某个数字出现了2遍以上
在这里我选择用map进行判断
每个数都存入map中,map自行计数
如果出现第三个数就将后面的数前移
代码:
class Solution {
public int removeDuplicates(int[] nums) {
int n=nums.length;
if(n==0) return 0;
int count=0;
Map<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i=0;i<n-count;i++){
if(!map.containsKey(nums[i])) map.put(nums[i],0);
if(map.get(nums[i])==2){
for(int j=i;j<n-1;j++){
nums[j]=nums[j+1];
}
count++;
i--;
continue;
}
map.put(nums[i],map.get(nums[i])+1);
}
return n-count;
}
}