数组中只出现一次的两个数字

 

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        //数组遍历,放入map中,value默认为1,如果有重复的数字,那么map的value加1
        //遍历map,找出value位1的k
        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        for(int i=0; i<array.length; i++){
            int tmp = array[i];
            if(!map.containsKey(tmp)){
                map.put(tmp,1);
            }else{
                map.put(tmp, map.get(tmp)+1);
            }
        }
        int[] re = new int[2];
        int j=0;
        for(Integer tmp: map.keySet()){
            if(map.get(tmp).equals(1)){
                re[j] = tmp;
                j++;
            }
        }
        return re;

    }
}

 

posted @   northli  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示