287 Find the Duplicate Number

使用哈希表,帮助找到重复元素

Java

class Solution {
    public int findDuplicate(int[] nums) {
        Set<Integer> seen = new HashSet<Integer>();
        for(int num : nums){
            if(seen.contains(num))
                return num;
            seen.add(num);
        }
        return -1;
    }
}

results matching ""

    No results matching ""