2015年11月3日星期二

Leetcode 217 Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Solution 1: easy hash problem
 public class Solution {  
   public boolean containsDuplicate(int[] nums) {  
     Set<Integer> set=new HashSet<>();  
     for (int x:nums) {  
       if (set.contains(x)) return true;  
       else set.add(x);  
     }  
     return false;  
   }  
 }  

没有评论:

发表评论