[Array]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.
思路:只要一个整数数组中某个元素出现两次及以上,返回true,否则返回false。将数组进行排序,只要找到出现两次的元素就可以了。
另外,需要注意的是,找到出现两次的元素的思想是,nums[i]=nums[i+1],这样会忽略数组长度为0或者1的情况,因此应该将这两种情况单独列出来。
bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false ;
int n = nums.size();
if(n == )//或者将这里写成if(n == 0 || n == 1)
return false;
sort(nums.begin(), nums.end());
for(int i = ; i < n; i++){
if(nums[i] == num[i+])
return true;
}
else if (i == n-)
return false;
}
[Array]217.Contains Duplicate的更多相关文章
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- 217. Contains Duplicate(C++)
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- LeetCode Array Easy 217. Contains Duplicate
Description Given an array of integers, find if the array contains any duplicates. Your function sho ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
随机推荐
- 【转载】Kafka介绍及升级经验分享
http://blog.talkingdata.net/?p=3165 背景 当时的现状:开始使用Kafka的时候,使用的版本是0.7.2,当时的目的是为了替代kestrel,主要是使用Kafka来做 ...
- LVS/Nginx/HAProxy负载均衡器的对比分析
转自:http://www.blogjava.net/ivanwan/archive/2013/12/25/408014.html LVS的特点是: 抗负载能力强.是工作在网络4层之上仅作分发之用,没 ...
- C++:多线程002
https://blog.csdn.net/morewindows/article/details/7442333 程序描述:主线程启动10个子线程并将表示子线程序号的变量地址作为参数传递给子线程.子 ...
- python+selenium中webdriver相关资源
Chrome chrome的webdriver : http://chromedriver.storage.googleapis.com/index.html chrome的webdriver需要对 ...
- 「题解」NOIP模拟测试题解乱写I(29-31)
NOIP模拟29(B) T1爬山 简单题,赛时找到了$O(1)$查询的规律于是切了. 从倍增LCA那里借鉴了一点东西:先将a.b抬到同一高度,然后再一起往上爬.所用的步数$×2$就是了. 抬升到同一高 ...
- JavaSE_12_序列化流和打印流
1.1 序列化流概述 Java 提供了一种对象序列化的机制.用一个字节序列可以表示一个对象,该字节序列包含该对象的数据.对象的类型和对象中存储的属性等信息.字节序列写出到文件之后,相当于文件中持久保存 ...
- 三次面试总结以及今后的todolist
金三银四跳槽季,按耐不住蠢蠢欲动的跳槽心,投了好多家的前端招聘,目前面了三家,有把握的零家.古人吾日三省吾身,我没那么高的觉悟,三面省一下自身,太咸鱼了是的我就是这么觉得的. 第一家公司在景田,很远, ...
- ios与android设备即时语音互通的录音格式预研说明
本文虽属原创,但是内容都是来自于网络,参考了大家的微博,以及论坛的总结. 在做语音对讲的时候,将会碰到录制语音格式的问题,因为要考虑自己开发设备的支持的格式,还要考虑其他设备操作系统的支持的格式,以及 ...
- ROC曲线及AUC
ROC曲线 意义 ROC曲线指受试者工作特征曲线 / 接收器操作特性曲线(receiver operating characteristic curve),是反映敏感性和特异性连续变量的综合指标,是用 ...
- PageRank算法R语言实现
PageRank算法R语言实现 Google搜索,早已成为我每天必用的工具,无数次惊叹它搜索结果的准确性.同时,我也在做Google的SEO,推广自己的博客.经过几个月尝试,我的博客PR到2了,外链也 ...