【一天一道LeetCode】#217. Contains Duplicate
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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
解题思路:
最开始想到的解法时利用STL的set,遍历数组,如果在set中找到则返回true,如果set中没有就添加到set中,如果最后都没有返回,那就返回false。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len = nums.size();
if(len ==0) return false;
set<int> st;
for(int i = 0 ; i < len ; i++){
auto iter = st.find(nums[i]);
if(iter!=st.end()){
return true;
}
else {
st.insert(nums[i]);
}
}
return false;
}
};
还有一种解法时首先对数组进行排序,然后遍历数组,如果出现与上一个数相等就返回true,如果遍历完数组都没有返回,就返回false。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len = nums.size();
if(len==0||len==1) return false;
sort(nums.begin(),nums.end());
for(int i =1 ; i <len ; i++){
if(nums[i]==nums[i-1]) return true;
}
return false;
}
};
【一天一道LeetCode】#217. Contains Duplicate的更多相关文章
- 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 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 ...
- [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 (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...
- Java for 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
Problem: Given an array of integers, find if the array contains any duplicates. Your function should ...
- (easy)LeetCode 217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
随机推荐
- JVM学习记录-对象已死吗
前言 先来回顾一下,在jvm运行时数据区,分为两部分,一个部分是线程共享区,主要包括堆和方法区,另一部是线程私有区分包括本地方法栈,虚拟机栈和程序计数器.在线程私有部分的三个区域是随着线程生和灭的.栈 ...
- Mysql安装的详细教程
首先,针对本人最近几天各种电脑安装数据库失败,反思总结所有的方式.现写出详细教程,希望别的人少走弯路. 首先 这次内容分为如下几步 : 1.mysql之前安装的彻底清除 2.mysql版本的选取 3. ...
- 【转载】 HTTP 中 GET 与 POST 的区别
HTTP 中 GET 与 POST 的区别 GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通 ...
- c语言的第四次作业
(一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...
- TensorFlow 聊天机器人开源项目评测第一期:DeepQA
聊天机器人开源项目评测第一期:DeepQA https://github.com/Conchylicultor/DeepQA 用 i5 的笔记本早上运行到下午,跑了 3 轮的结果,最后效果并不理想.官 ...
- MongoDb进阶实践之六 MongoDB查询命令详述(补充)
一.引言 上一篇文章我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来.如果大家想看上一篇有关MongoDB查询的 ...
- dva-quickstart 与 create-react-app 比较(一)
最近在学习 React , 现对 dva-quickstart 与 create-react-app 比较 1. 安装, 两个都需要安装工具包:npm install -g create-re ...
- Eclipse插件安装4种方法
第一种:直接复制法 假设Eclipse的安装目录在C:\eclipse,解压下载的eclipse 插件或者安装eclipse 插件到指定目录AA(如:c:\AA)文件夹,打开AA 文件夹,在AA文件夹 ...
- Django笔记--模型
ORM是"对象-关系-映射"的简称,在Django当中,ORM就是模型类的管理器对象.操作顺序是先定义模型类,再定义模型类管理器,然后在模型类中实例化一个模型类管理器的对象,作为模 ...
- Day 2 Python数值计算
一.数值数据类型 在Python中,数值数据类型有以下两种: 整数 整数用"int"数据类型表示.int类型的数据可以是正数也可以是负数,Python可以处理任意大小的整数. 浮点 ...