LeetCode Array Easy 217. Contains Duplicate
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: [,,,]
Output: trueExample 2:
Input: [,,,]
Output: falseExample 3:
Input: [,,,,,,,,,]
Output: true
问题描述:给定一个数组,判断数组中是否存在重复元素,如果存在返回true,如果不存在返回false
思路:使用HashSet保存数组中的每个元素,如果在保存时HashSet中已经存在该元素,则返回true。
public bool ContainsDuplicate(int[] nums) {
var hashSet = new HashSet<int>();
for(int i = ; i < nums.Length; i++){
if(hashSet.Add(nums[i])==false)
return true;
}
return false;
}

LeetCode Array Easy 217. Contains Duplicate的更多相关文章
- LeetCode Array Easy 219. Contains Duplicate II
---恢复内容开始--- Description Given an array of integers and an integer k, find out whether there are two ...
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- [LeetCode&Python] Problem 217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- LeetCode Array Easy 485. Max Consecutive Ones
Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...
- LeetCode Array Easy 448. Find All Numbers Disappeared in an Array
Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear ...
- LeetCode Array Easy 414. Third Maximum Number
Description Given a non-empty array of integers, return the third maximum number in this array. If i ...
- LeetCode Array Easy 283. Move Zeroes
Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- LeetCode Array Easy 189. Rotate Array
---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...
随机推荐
- Serilog 自定义 Enricher 来增加记录的信息
原文:Serilog 自定义 Enricher 来增加记录的信息 Serilog 自定义 Enricher 来增加记录的信息 Intro Serilog 是 .net 里面非常不错的记录日志的库,结构 ...
- Vue-基础(一)
一.Vue中的常用指令 什么是指令? 指令就是vue中提供的一些对于页面和数据更为方便的操作, 指令就是以数据去驱动DOM行为的,简化DOM的操作 常用指令 v-text / v-html :用于为标 ...
- Vue小白篇 -Vue 的模板语法
可以插入任何你想插入的内容,除了 if-else if-else用三元运算符代替 <div id="box"> <!--模板语法--> <h2> ...
- 【转】CentOS 7.3 从下载到安装
CentOS 7.3 从下载到安装 https://blog.csdn.net/sxy2475/article/details/75194142 [百度知道]图解CentOS 7.3安装步骤 ht ...
- Linux Shell脚本在service模式下的环境变量
普通Shell脚本在终端下运行时,环境变量来自于init/profile/.bash_profile等,就是在当前终端下可以得到的环境,比如PATH.在service模式下,脚本运行在受限的环境中:比 ...
- 转 HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
- webpack 学习三 模式
开发环境(development)和生产环境(production)的构建目标差异很大.在开发环境中,我们需要具有强大的.具有实时重新加载(live reloading)或热模块替换(hot modu ...
- RGB和十六进制转换
1.十六进制换RGB 例: var color = '#69ad52' let r = parseInt(“0px” + color.slice(1, 3)) //105 let g = pars ...
- JUC线程池
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11443644.html newFixedThreadPool 重用指定数目(nThreads)的线程, ...
- shell 输入输出重定向
1. 命令列表: command > file 将输出重定向到file command < file 将输入重定向到file command >> file 将输出以追加的方式 ...