标签:
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.
public class Solution { public boolean containsDuplicate(int[] nums) { //用一个set保存数组中的值,如果发现当前值已经在set中存在,则返回true。注意不存在时需要add到set Set<Integer> set=new HashSet<Integer>(); for(int i=0;i<nums.length;i++){ if(!set.isEmpty()&&set.contains(nums[i])){ return true; } set.add(nums[i]); } return false; } }
[leedcode 217] Contains Duplicate
标签:
原文地址:http://www.cnblogs.com/qiaomu/p/4709363.html