热身1---只出现一次的数字

题目描述:

import java.util.HashSet;
class Solution {
    public int singleNumber(int[] nums) {
        Set<Integer> set=new HashSet<>();
        for(int i=0;i<nums.length;i++){
            if(!set.add(nums[i])){//HashSet添加重复元素会返回false
                set.remove(nums[i]);
            }
        }
        return set.iterator().next();//仅存一个即为所求
    }
}

 参考博客  https://www.cnblogs.com/zfLee/p/9330127.html

一开始没注意线性复杂度...

posted @ 2019-03-02 15:02  本尘  阅读(157)  评论(0)    收藏  举报