题目地址:Contains Duplicate - LeetCode


题目:
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

意思是给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。

我的想法是先排序,再看是否有重复的。
Python3代码如下:

class Solution:def containsDuplicate(self, nums: List[int]) -> bool:if len(nums)<2:return Falsenums.sort()for i in range(1,len(nums)):if nums[i]==nums[i-1]:return Truereturn False

结果如下:

Runtime: 52 ms, faster than 50.12% of Python3 online submissions for Contains Duplicate.
Memory Usage: 16.4 MB, less than 82.20% of Python3 online submissions for Contains Duplicate.

看了别人的Solution后,我觉得也许用set更快:

class Solution:def containsDuplicate(self, nums: List[int]) -> bool:nums2=set(nums)return  len(nums2)!=len(nums)

结果只用了44ms,快了11ms,但内存多占用了3M

Java代码如下:

class Solution {public boolean containsDuplicate(int[] nums) {Set<Integer> set = new HashSet<>(nums.length);for (int x: nums) {set.add(x);}return set.size()!=nums.length;}
}

耗时只有8ms,但是内存占用了44M。

LeetCode 217 Contains Duplicate--python,java解法--set--简单相关推荐

  1. LeetCode 85. Maximal Rectangle --python,java解法

    题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  2. LeetCode 965 Univalued Binary Tree--判断二叉树的所有节点的值是否相同--python,java解法

    题目地址:Univalued Binary Tree - LeetCode Acceptance: 67.6% Difficulty: Easy A binary tree is univalued ...

  3. LeetCode 961 N-Repeated Element in Size 2N Array --python,java解法

    题目地址:N-Repeated Element in Size 2N Array - LeetCode Acceptance:73.3% Difficulty:Easy In a array A of ...

  4. LeetCode 217. Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. LeetCode:110(Python)—— 平衡二叉树(简单)

    平衡二叉树 概述:给定一个二叉树,判断它是否是高度平衡的二叉树.一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 . 输入:root = [3,9,20,null ...

  6. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  7. LeetCode 136. Single Number--异或--Java,C++,Python解法

    题目地址:Single Number - LeetCode Given a non-empty array of integers, every element appears twice excep ...

  8. LeetCode 74. Search a 2D Matrix--有序矩阵查找--python,java,c++解法

    题目地址:Search a 2D Matrix - LeetCode Write an efficient algorithm that searches for a value in an m x ...

  9. LeetCode 102. Binary Tree Level Order Traversal--递归,迭代-Python,Java解法

    题目地址: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  10. LeetCode 77. Combinations--回溯法,-Python,Java解法

    题目地址: Given two integers n and k, return all possible combinations of k numbers out of 1 - n. Exampl ...

最新文章

  1. 大厂围猎春招,年轻人却卸甲出逃
  2. phpcms v9二次开发及使用中各种问题解决方案(一)
  3. 正确认识Arrays.asList方法
  4. agile organization
  5. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1082:求小数的某一位
  6. .NET Core 开源项目 Anet 在路上
  7. r语言清除变量_如何优雅地计算多变量 | R语言进阶
  8. android中利用实现二级联动的效果
  9. 退休的姐妹们,你们还打工吗?
  10. vivo手机通用的官方售后解锁工具包_一加全系列手机一键解锁BootLoader超详细图文刷机教程...
  11. JVM参数-X和-XX的区别
  12. 初探 Linux操作系统 (一):站在巨人的肩膀上
  13. raw图片python医学影像的格式转换
  14. U-boot2022.07 imx6q 移植 - SPL-DTC-DCD
  15. 几个免费的长链接缩短链接工具
  16. mac-mini系统安装
  17. Redis 一篇足以
  18. Windows上那些值得推荐的良心软件-整理
  19. 《白帽子讲web安全》读书笔记_2021年7月16日(2)_第3篇 服务器端应用安全
  20. USACO Training Section 3.2 Feed Ratios

热门文章

  1. Nat. Mach. Intell. | 可解释性人工智能(xAI)遇上药物发现
  2. J. Cheminform. | 基于化学基因组学中深度和浅层学习预测药物特异性
  3. Machine Learning | (6) Scikit-learn的分类器算法-性能评估
  4. 其他算法-高斯白噪声
  5. “完美论文”过于真实,道出了科研狗的痛
  6. 科研 | Nature:新型土壤细菌具有多种合成次级代谢物的基因
  7. pandas将dataframe中的年、月、日数据列合并成完整日期字符串、并使用to_datetime将字符串格式转化为日期格式
  8. pandas使用query函数和sample函数、使用query函数筛选dataframe中的特定数据行并使用sample函数获取指定个数的随机抽样数据
  9. R语言ggplot2可视化为组合图添加综合图例实战:使用ggpubr包ggarrange函数实现综合图例、使用patchwork包实现综合图例
  10. python使用TSNE为影像组学(radiomics)数据进行降维可视化分析