题解 | #数组中只出现一次的数(其它数出现k次)#

数组中只出现一次的数(其它数出现k次)

http://www.nowcoder.com/practice/5d3d74c3bf7f4e368e03096bb8857871

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *使用HashMap
     * 
     * @param arr int一维数组 
     * @param k int 
     * @return int
     */
    public int foundOnceNumber (int[] arr, int k) {
        // write code here
        //key表示数字,value表示是否已经出现
        HashMap<Integer,Boolean> hashMap = new HashMap<>();
        int length = arr.length;
        //存入hashmap
        for(int i = 0;i<length;i++){
            if(hashMap.containsKey(arr[i])){
                hashMap.put(arr[i],true);
            }else{
                hashMap.put(arr[i],false);
            }
        }
        Set<Integer> set = hashMap.keySet();//获取键集
        //遍历,取出只出现一次的数
        for(int i:set){
            if(hashMap.get(i) == false){
                return i;
            }
        }
        
        return 0;
    }
}
全部评论

相关推荐

AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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