原题链接在这里:https://leetcode.com/problems/contains-duplicate/
判断有没有重复,很多时候都是用HashSet, HashMap.
Tiem Complexity: O(n). Space: O(n).
AC Java:
1 public class Solution { 2 public boolean containsDuplicate(int[] nums) { 3 if(nums == null || nums.length == 0){ 4 return false; 5 } 6 Set<Integer> hs = new HashSet<Integer>(); 7 for(int i = 0; i<nums.length; i++){ 8 if(hs.contains(nums[i])){ 9 return true; 10 }else{ 11 hs.add(nums[i]); 12 } 13 } 14 return false; 15 } 16 }
原文:http://www.cnblogs.com/Dylan-Java-NYC/p/4966111.html