Problem 217

Contains Duplicate

Posted by Ruizhi Ma on May 19, 2019

问题描述

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.

Example 1:

Input: [1,2,3,1]
Output: true

解题思路

就是最简单的两层for循环,这个没什么可说的。

代码一

class Solution {
    public boolean containsDuplicate(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int index = i + 1; index < nums.length; index++) {
                if (nums[i] == nums[index]){
                    return true;
                }
            }
        }
        return false;
    }
}

效率探究

Runtime: 331 ms, faster than 6.85% of Java online submissions for Contains Duplicate.
Memory Usage: 41.2 MB, less than 74.78% of Java online submissions for Contains Duplicate.

解题思路

因为HashSet,不能存放重复的值,所以一旦有重复的值,就return true就可以了

问题解惑

如果不重复的情况,则在if判断里面同时处理了add操作,可谓一举两得。

代码二

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> a = new HashSet<>(2 * nums.length);
        for (int i: nums){
            if (!a.add(i)) return true;
        }
        return false;
    }
}

效率探究

Runtime: 4 ms, faster than 96.59% of Java online submissions for Contains Duplicate.
Memory Usage: 41.3 MB, less than 71.70% of Java online submissions for Contains Duplicate.

小感想

注意:感觉最近经常看到HashSet,HashMap,List之类的使用,这几天有空补一补这方面的知识。