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

来源:互联网 发布:判断数据上升趋势算法 编辑:程序博客网 时间:2024/04/29 02:00

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


0 0
原创粉丝点击