HashMap做法

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

http://www.nowcoder.com/questionTerminal/389fc1c3d3be4479a154f63f495abff8

HashMap做法,时间复杂度O(n)

public int[] FindNumsAppearOnce (int[] array) {

    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int[] res = new int[2];
    int count = 0;
    for(int i=0;i<array.length;i++){
        if(map.get(array[i])!=null){
            map.put(array[i],map.get(array[i])+1);
        }
        else{
            map.put(array[i],1);
        }
    }
    for(int i:map.keySet()){
        if(map.get(i)==1){
            res[count] = i;
            count++;
        }
    }
    return res;
}
全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务