leetcode 217
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.
题意:判断数组中是否有重复的元素,如果没有返回false,反之返回true.
解法:先对数组进行排序,然后比较排序之后数组相邻元素。
代码如下:
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int size = nums.size();
if(size == || size == )
{
return false;
}
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i++)
{
if(nums[i-] == nums[i])
{
return true;
}
}
return false;
}
};
leetcode 217的更多相关文章
- leetcode——217. 存在重复元素
leetcode--217. 存在重复元素 题目描述:给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false ...
- [LeetCode]-217.存在重复元素-简单
217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false . 示例 1: 输入: [1, ...
- 25. leetcode 217. Contains Duplicate
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 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 ...
- LN : leetcode 217 Contains Duplicate
lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...
- 2017-3-11 leetcode 217 219 228
ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- Java实现 LeetCode 217 存在重复元素
217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3 ...
随机推荐
- [课程设计]Scrum 1.2 Spring 计划&系统流程&DayOne燃尽图
多鱼点餐系统WEB Spring 计划 ● 产品BACKLOG 多鱼点餐系统产品BACKLOG ID Name Imp Est How to demo Notes 1 设计框架结构 10 8 利用美学 ...
- POJ2778 DNA sequence
题目大意:给出m个疾病基因片段(m<=10),每个片段不超过10个字符.求长度为n的不包含任何一个疾病基因片段的DNA序列共有多少种?(n<=2000000000) 分析:本题需要对m个疾 ...
- 用Ant来做一键部署
部署Java Web项目到远程服务器上,以前经常用的操作方式: 1.在eclipse上导出项目war包 2.把war包通过ftp方式传到服务器上,比如Tomcat的webapps目录下 3.启动tom ...
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4 ...
- Oracle补习班第十天
Life without love is like a tree without blossoms or fruit. 缺少爱的生活就像从未开花结果的枯树 RMAN备份工具 crosscheck ba ...
- Tplink客户端设置
之前在JD上面买了个Tplink,将公司的无线网转成有线的给我的台式机用,可是突然就掉线了,怎么配置都不行.甚至按向导去做了,后来连配置界面都进不去.然后又再某宝上面买了两个,回来配置也发现了这个情况 ...
- Ubuntu16.10 主题flatabulous安装
以前通过apt直接安装的flatabulous主题16.10上安装会出现找不到包的情况,可以采用源码安装 原来的apt-get方式: 1:主题 sudo add-apt-repository ppa: ...
- 学习opencv
图像缩放 cv::Mat src_img = cv::imread(); ; cv::Mat dst_img1; cv::Mat dst_img2(src_img.rows*0.5, src_img. ...
- JVM实用参数(七)CMS收集器
HotSpot JVM的并发标记清理收集器(CMS收集器)的主要目标就是:低应用停顿时间.该目标对于大多数交互式应用很重要,比如web应用.在我们看一下有关JVM的参数之前,让我们简要回顾CMS收集器 ...
- DEDE后台登录和前台验证码不显示的解决方法
DEDE后台登录和前台验证码不显示的解决方法,网络上现在有好几种方法,某些时候还是有用的.说说自己今天遇到的一个情况,跟其他不一样的原因和解决方法: 方法一 1.用ftp把网站根目录下的DATA文件 ...