LeetCode 217. Contains Duplicate

原创 2018年04月16日 14:05:45

问题描述

  • 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)
  • 不用额外空间
    先排序。然后固定一个数,对剩下的数进行二分查找 O(NlogN)

代码实现

class Solution {
    public boolean containsDuplicate(int[] nums) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (! set.add(nums[i])) {
                return true;
            }
        }
        return false;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79959818

Windows优化大师注册机源码

Windows优化大师注册机源码unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Co...
  • Drate
  • Drate
  • 2003-02-25 13:01:00
  • 2102

LeetCode 217. Contains Duplicate(C++版)

Given an array of integers, find if the array contains any duplicates. Your function should return t...
  • Mary19920410
  • Mary19920410
  • 2017-03-31 11:16:32
  • 305

leetcode 217 Contains Duplicate 数组中是否有重复的数字

Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an a...
  • wangyaninglm
  • wangyaninglm
  • 2015-08-09 21:48:10
  • 2484

leetcode【217+219 Contains Duplicate I+II】【python】

这两道题都是让我们找到数组中是否有重复的数字,这种查找题首先就要考虑hash查找了,毕竟快。 217这道直接用dict就好,出现超过两次直接返回即可。class Solution(object): ...
  • u014251967
  • u014251967
  • 2016-09-08 10:33:49
  • 233

leetcode 217: Contains Duplicate

leetcode 217: Contains Duplicate c++ java python
  • xudli
  • xudli
  • 2015-06-05 08:27:02
  • 2687

LeetCode 217:Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return t...
  • sunao2002002
  • sunao2002002
  • 2015-05-31 18:37:10
  • 7142

【LeetCode-面试算法经典-Java实现】【217-Contains Duplicate(包含重复元素)】

【217-Contains Duplicate(包含重复元素)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-C...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-28 07:00:49
  • 3004

219. Contains Duplicate II [easy] (Python)

题目链接https://leetcode.com/problems/contains-duplicate-ii/题目原文 Given an array of integers and an int...
  • coder_orz
  • coder_orz
  • 2016-06-14 19:59:14
  • 1954

LeetCode 217. Contains Duplicate(检查重复)

原题网址:https://leetcode.com/problems/contains-duplicate/ Given an array of integers, find if the arra...
  • jmspan
  • jmspan
  • 2016-05-06 04:56:24
  • 286

leetcode_c++:Contains Duplicate II(219)

题目Given an array of integers and an integer k, find out whether there are two distinct indices i and...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-06-18 13:33:40
  • 311
收藏助手
不良信息举报
您举报文章:LeetCode 217. Contains Duplicate
举报原因:
原因补充:

(最多只允许输入30个字)