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.

链接: http://leetcode.com/problems/contains-duplicate/

2/25/2017, Java

 1 public class Solution {
 2     public boolean containsDuplicate(int[] nums) {
 3         if (nums.length == 0) return false;
 4         HashSet<Integer> s = new HashSet<Integer>();
 5         
 6         for(int i = 0; i < nums.length; i++) {
 7             if (s.contains(nums[i])) return true;
 8             s.add(nums[i]);
 9         }
10         return false;
11     }
12 }

 

posted @ 2017-02-26 06:28  panini  阅读(110)  评论(0编辑  收藏  举报