当前位置: 首页 > news >正文

[Swift]LeetCode217. 存在重复元素 | Contains Duplicate

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9745536.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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。

示例 1:

输入: [1,2,3,1]
输出: true

示例 2:

输入: [1,2,3,4]
输出: false

示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

168ms
1 class Solution {
2     func containsDuplicate(_ nums: [Int]) -> Bool {
3         return Set(nums).count != nums.count
4     }
5 }

176ms

 1 class Solution {
 2     func containsDuplicate(_ nums: [Int]) -> Bool {
 3         
 4         if nums.count == 0 {
 5             return false
 6         }
 7         
 8         var dict: Set<Int> = Set()
 9         
10         for item in nums {
11             dict.insert(item)
12         }
13         
14         if dict.count < nums.count {
15             return true
16         }
17         
18         
19         return false
20     }
21 }

180ms

 1 class Solution {
 2     func containsDuplicate(_ nums: [Int]) -> Bool {
 3         guard nums.count > 1 else {
 4             return false
 5         }
 6         var  set = Set([Int]())
 7         for num in nums {
 8             if set.contains( num) {
 9                 return true
10             } else {
11                 set.insert(num)
12             }
13         }
14         return false
15     }
16 }

 

转载于:https://www.cnblogs.com/strengthen/p/9745536.html

相关文章:

  • 网管日志-06.07.18
  • unity 中 Tilemap的使用 笔记
  • 正版和盗版对开发的影响(请注意这个问题)
  • github上更新fork项目
  • 基于DBDataAccess类的具体数据访问类,这些代码大部分都可以自动生成。
  • 通俗易懂系列 | 设计模式(八):建造者模式
  • O血型的性格
  • entity
  • 01-python学习之路
  • 企业网站服务器负载均衡技术
  • 玩转报表排名之组内排名
  • 觉得MemberShip的灵活性太低了,不知各位有什么招弥补呢?
  • Primitives vs Objects
  • 微软专家:Linux不会对Windows构成威胁---怀疑中~
  • [BZOJ 4598][Sdoi2016]模式字符串
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • Angular Elements 及其运作原理
  • js递归,无限分级树形折叠菜单
  • Redis 中的布隆过滤器
  • 如何利用MongoDB打造TOP榜小程序
  • 入门级的git使用指北
  • 学习HTTP相关知识笔记
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • #1014 : Trie树
  • #if #elif #endif
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (层次遍历)104. 二叉树的最大深度
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • .NET Core/Framework 创建委托以大幅度提高反射调用的性能
  • .NET delegate 委托 、 Event 事件
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)
  • .Net6 Api Swagger配置
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • @Bean, @Component, @Configuration简析
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • @private @protected @public
  • @取消转义
  • [ C++ ] STL_list 使用及其模拟实现
  • []C/C++读取串口接收到的数据程序
  • [20171113]修改表结构删除列相关问题4.txt
  • [2021 蓝帽杯] One Pointer PHP
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [AutoSar]BSW_OS 01 priority ceiling protocol(PCP)
  • [C#] 如何调用Python脚本程序
  • [C#]使用DlibDotNet人脸检测人脸68特征点识别人脸5特征点识别人脸对齐人脸比对FaceMesh
  • [HNOI2008]玩具装箱toy
  • [Linux]使用CentOS镜像与rpm来安装虚拟机软件
  • [MySQL FAQ]系列 -- 如何利用触发器实现账户权限审计
  • [nginx] 网上最全面nginx教程(近100篇文章整理)
  • [pyqt5]PyQt5窗体背景图片拉伸填充
  • [R] data.frame() creates list?