删除排序数组中的重复数字 II
跟进“删除重复数字”:
如果可以允许出现两次重复将如何处理?
样例
给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]。
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if(nums == null || nums.length == 0) return 0;
int reslen = 0;
int j = reslen;
int len = nums.length;
Map<Integer,Integer> times = new HashMap<>();
//int[] times = new int[len]; 有负数的情况
for(int i = 0 ; i < len; i++){
int cou = 0;
if(times.containsKey(nums[i])){
cou = times.get(nums[i]);
}else{
times.put(nums[i],cou);
}
if(cou < 2){
nums[j++] = nums[i];
// times[nums[i]] += 1;
times.put(nums[i], ++cou);
} else{
// times[nums[i]] += 1;
times.put(nums[i], ++cou);
}
}
reslen = j;
return reslen;
}
}