Problem 260

Posted by Ruizhi Ma on October 27, 2020

Solution URL

https://leetcode.com/submissions/detail/414020977/

代码

class Solution {
    public int[] singleNumber(int[] nums) {
        //HashMap法
        Map<Integer, Integer> map = new HashMap<>();
        //遍历nums,将num和出现的频率以k-v保存
        for(int num: nums){
            map.put(num, map.getOrDefault(num, 0) + 1);
        }

        //遍历map,找到v为1的k
        int[] res = new int[2];
        int i = 0;
        for(Integer key: map.keySet()){
            if(map.get(key) == 1) res[i++] = key;
        }

        return res;
    }
}

0 comments
Anonymous
Error: Bad credentials.
Markdown is supported

Be the first person to leave a comment!