【一天一道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 ...
随机推荐
- CSS :focus 选择器
:focus 选择器用于选取获得焦点的元素. <!DOCTYPE html> <html> <head> <style> input:focus { b ...
- ORACLE设置自启动记录
设置开机自启动1. 修改Oracle系统配置文件:/etc/oratab,只有这样,Oracle 自带的dbstart和dbshut才能够发挥作用.[root@hailiang ~]# vi /etc ...
- jmeter录制APP时不能登录的问题
问题描述: 录制APP时,其他一切挺顺利的,但在登录的时候提示"服务器发生未知错误,请稍后重试".一开始以为是接口问题,用python脚本调用了下,是可以登录的,排除接口问题.百度 ...
- Oracle 11g 中SQL性能优化新特性之SQL性能分析器(SQLPA)
Oracle11g中,真实应用测试选项(the Real Application Testing Option)提供了一个有用的特点,叫SQL性能分析器(SQL Performance Analyze ...
- centos6 安装redis-4.0.9
从redis官网下载Linux redis4.0.9版本,我下载的redis-4.0.9.tar.gz(目前最新稳定版),下载到/usr/local/src目录,如果没有就mkdir创建一个. 下载链 ...
- Python中enumerate用法详解
enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)e ...
- Go 语言类型转换
类型转换用于将一种数据类型的变量转换为另外一种类型的变量.Go 语言类型转换基本格式如下: type_name(expression) type_name 为类型,expression 为表达式. 实 ...
- Android自定义View(CustomCalendar-定制日历控件)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeas ...
- OpenCV +Python 制作画板
效果图 画图工具实现 代码 运行结果 程序分析 窗体自由度 如何退出程序 滚动条相关 支持的事件 首先声明一下,本例思路不是博主原创,博主在前人的代码上进行了个性化的修改,制作了一个简单的画图工具.下 ...
- Unity3d导出场景地图寻路
Unity3d导出场景地图寻路(金庆的专栏)Unity3d中用无渲染的透明盒子摆出地面和阻档区域. this.renderer.enabled = false;所有这些盒子设为Navig ...