作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/contains-duplicate/

Total Accepted: 86528 Total Submissions: 209877 Difficulty: Easy

题目描述

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

Example 2:

Input: [1,2,3,4]
Output: false

Example 3:

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

题目大意

判断是否有重复元素.

解题方法

这个题想到了五种解法。

方法一,两个for循环排序,肯定效率低。

方法二,用ArrayList,判断在已经添加的元素中是否要插入的这个元素。事实证明这个方法超时。

方法三,用HashMap,统计词频最高的是不是1。

方法四,用HashSet判断set之后长度是否变短。

方法五,排序,看排序之后相邻的元素是不是有相等。

字典统计词频

使用HashMap:

public class Solution {
public boolean containsDuplicate(int[] nums) {
HashMap map = new HashMap();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
return true;
} else {
map.put(nums[i], nums[i]);
}
}
return false;
}
}

AC:17ms

python版本:

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if not nums: return False
count = collections.Counter(nums)
return count.most_common(1)[0][1] > 1

使用set

使用HashSet:

public class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set=new HashSet();
for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
return true;
} else {
set.add(nums[i]);
}
}
return false;
}
}

AC:16ms

查阅资料,此处应该使用HashSet,因为HashMap存储时是对键值对进行存储,如果用一个无穷,不重复的数组进行判断,复杂度与时间消耗是很多的。
而HashSet的好处在于:HashSet实现了Set接口,它不允许集合中有重复的值,在进行存储时,先进行判断,使用contain方法即可,复杂度与时间消耗就随之降下来了。

python版本:

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(nums) != len(set(nums))

排序

排序判断相邻的元素是否相等,即可知道是否有重复了。

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if not nums: return False
nums.sort()
return any(nums[x] - nums[x + 1] == 0 for x in xrange(len(nums) - 1))

日期

2016/5/1 10:55:17
2018 年 11 月 13 日 —— 时间有点快

【LeetCode】217. Contains Duplicate 解题报告(Java & Python)的更多相关文章

  1. LeetCode 217 Contains Duplicate 解题报告

    题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  4. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  5. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  6. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  7. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  9. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

随机推荐

  1. Python基础笔记1

    这篇笔记来自廖雪峰的Python教程. 一.Python基础 Python使用缩进来组织代码块,务必遵守约定俗成的习惯,坚持使用4个空格的缩进. 在文本编辑器中,需要设置把Tab自动转换为4个空格,确 ...

  2. 毕业设计之LNP+DISCUZ +分离的数据库操作

    环境介绍: CentOS6.9最小化安装 https://nginx.org/download/nginx-1.16.1.tar.gz https://www.php.net/distribution ...

  3. sqlalchemy模块的基本使用

    Python中SQLAlchemy模块通过建立orm来对数据库进行操作 1. 建表 方式1 # -*- coding:utf-8 -*- # Author:Wong Du from sqlalchem ...

  4. 准确率,召回率,F值,ROC,AUC

    度量表 1.准确率 (presion) p=TPTP+FP 理解为你预测对的正例数占你预测正例总量的比率,假设实际有90个正例,10个负例,你预测80(75+,5-)个正例,20(15+,5-)个负例 ...

  5. A Child's History of England.39

    He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...

  6. C逗号表达式

    c语言提供一种特殊的运算符,逗号运算符,优先级别最低,它将两个及其以上的式子联接起来,从左往右逐个计算表达式,整个表达式的值为最后一个表达式的值.如:(3+5,6+8)称为逗号表达式,其求解过程先表达 ...

  7. 转Android service 启动篇之 startForegroundService

    本文转自:https://blog.csdn.net/shift_wwx/article/details/82496447 前言: 在官方文档 Android 8.0 行为变更 中有这样一段话: An ...

  8. spring boot项目创建与使用

    概述 spring boot通常使用maven创建,重点在于pom.xml配置,有了pom.xml配置,可以先创建一个空的maven项目,然后从maven下载相关jar包. spring boot d ...

  9. Function overloading and return type

    In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】备忘录模式Memento

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/memento.py #!/usr/bin/env pyt ...