Java实现-删除排序数组中的重复数字2

来源:互联网 发布:新津知艺术馆 编辑:程序博客网 时间:2024/06/06 04:32


public class Solution {    /**     * @param A: a array of integers     * @return : return an integer     */    public int removeDuplicates(int[] nums) {        // write your code here        if(nums.length==0){return 0;}LinkedHashMap<Integer,Integer> map=new LinkedHashMap<Integer,Integer>();for(int i=0;i<nums.length;i++){if(map.containsKey(nums[i])){map.put(nums[i], map.get(nums[i])+1);}else{map.put(nums[i], 1);}}Set<Integer> set=map.keySet();int count=0;Iterator<Integer> it=set.iterator();while(it.hasNext()){int temp=it.next();if(map.get(temp)>=2){nums[count]=temp;count++;nums[count]=temp;count++;}else{nums[count]=temp;count++;}}return count;    }}


原创粉丝点击