【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛
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)的更多相关文章
- LeetCode 217 Contains Duplicate 解题报告
题目要求 Given an array of integers, find if the array contains any duplicates. Your function should ret ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
随机推荐
- FVCOM泥沙模块河流边界处理
简介 入流河流携带泥沙可以按照节点和边界两种形式给定,这两种方法都是在相关的节点上进行直接赋值,并不能保证进入计算域内泥沙总体积. 相关设置 XX_run.nml 河流参数设置 &NML_RI ...
- Redis高并发处理常见问题及解决方案
1. 大型电商系统高流量系统设计 场景: 大量电商系统每天要处理上亿请求,其中大量请求来自商品访问.下单.商品的详情是时刻变化,由于请求量过大,不会频繁去服务端获取商品信息,导致服务器压力极大.需要用 ...
- 亿级Web系统搭建:单机到分布式集群
亿级Web系统搭建:单机到分布式集群 当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压 ...
- 一站式Flink&Spark平台解决方案——StreamX
大家好,我是独孤风.今天为大家推荐的是一个完全开源的项目StreamX.该项目的发起者Ben也是我的好朋友. ****什么是StreamX,StreamX 是Flink & Spark极速开发 ...
- [C++] vptr, where are you?
Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...
- python下载openpyxl
直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...
- c++string转const char*与char*
#include <iostream> #include <string> #include <memory> using namespace std; const ...
- oracle 当月日历的sql
select max(sun) sun, max(mon) mon, max(tue) tue, max(wed) wed, max(thu) thu, max(fri) fri, max(sat) ...
- Linux系统下部署eleasticsearch+kibana
1.官网下载eleasticsearch和kibana,两个版本应安装一致,否则会出现kibana连接不上eleasticsearch的情况(这里我以6.3.1为例) eleasticsearch的下 ...
- Redis集群环境各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的解决方式
总结/朱季谦 在搭建Redis5.x版本的集群环境曾出现各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的情况,故而把解决方式记录下来. 在以下 ...