Contains Duplicate

https://leetcode.com/problems/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.

解题思路:

新题,简单的set题目,时间复杂度和空间复杂度都是O(n)。

public class Solution {

    public boolean containsDuplicate(int[] nums) {

        Set<Integer> set = new HashSet<Integer>();

        for(int i = 0; i < nums.length; i++) {

            if(set.contains(nums[i])) {

                return true;

            }

            set.add(nums[i]);

        }

        return false;

    }

}

 

你可能感兴趣的:(contains)