删除排序数组中的重复数字 II

 删除排序数组中的重复数字 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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值