LN : leetcode 217 Contains Duplicate
lc 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.
方法一 Time Limit Exceeded##
首先想到的就是比较暴力,没有技巧性可言的时间复杂度为O(\(n^{2}\))的方法。显而易见,这个方法非常没有效率,通不过测试, Time Limit Exceeded。
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.empty()) return false;
for (int i = 0; i < nums.size(); i++) {
for (int j = i+1; j < nums.size(); j++) {
if (nums[i] == nums[j]) return true;
}
}
return false;
}
};
方法二 Time Limit Exceeded##
运用迭代器iterator的find()函数来查找是否有重复的整数。虽然看上去只有一层循环,但是find()本身就相当于一次循环,所以算法的时间复杂度并没有提高,依旧是O(\(n^{2}\))
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
for (int i = nums.size()-1; i > 0; i--) {
int temp = nums[i];
nums[i] = NULL;
vector<int>::iterator result = find( nums.begin( ), nums.end( ), temp);
if (result != nums.end()) return true;
nums[i] = temp;
}
return false;
}
};
方法三 Accepted##
利用STL中set中元素都是唯一的这一特点,可以用来去重。以此对比vector和set中的数据个数,可以说是很巧妙的方法了。
#include <set>
using namespace std;
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size() > set<int>(nums.begin(), nums.end()).size();
}
};
LN : 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 ...
- [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 ...
随机推荐
- JSP的客户端请求
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/client-request.html: 当浏览器请求一个网页时,它向Web服务器发送大量的信息,信息不能 ...
- PS如何绘制虚线圆
1 绘制一个圆的路径 2 选择铅笔工具,然后点击"画笔笔尖形状",选好笔尖的直径和间距(不同的直径对应不同的间距,没有标准数值,自己推拉滑动条就可以了) 3 在路径选项卡, ...
- 怎样在Swift中使用NSError
步骤一:声明NSError变量. 一定要加"?",不加或者加"!"都不行.由于使用了optional,所以要用var而不用let. var error: NSE ...
- Wordpress 建站(一)
去年在美国的justhost上买了两个域名(shanyexuanyu.com 和 chenjinyu.net.shanyexuanyu.com是给一位马来西亚的佛教徒朋友做的站点. 她镜头下佛教的文 ...
- java并发编程之Semaphore
信号量(Semaphore).有时被称为信号灯.是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们可以正确.合理的使用公共资源. 一个计数信号量.从概念上讲,信号量维护了一个许可集.如 ...
- 通过java类文件识别JDK编译版本号
类文件里第5,6.7,8四个字节是jDK版本信息.当中5,6为小版本:7,8为大版本. 大版本号号相应JDK版本号例如以下: JDK版本 7,8字节 JDK8 52(0x34) JDK7 51(0x3 ...
- 洛谷 P1525 关押罪犯==codevs 1069 关押罪犯[NOIP 2010]
P1525 关押罪犯 513通过 1.4K提交 题目提供者该用户不存在 标签图论并查集NOIp提高组2010 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 咳咳.竟MLE了. 囧.运行时错误 ...
- H264 层次构成[2]
H264层次构成 H264标准是由JVT(Joint Video Team,视频联合工作组)组织提出的新一代数字视频编码标准.JVT于2001年12月在泰国Pattaya成立.它由ITU-T的VCEG ...
- 服务器可用的Socket
"; IPAddress ServerIp = IPAddress.Parse("112.124.46.251"); IPEndPoint iep = new IPEnd ...
- 【Silverlight】Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps
[Silverlight]Bing Maps学习系列(八):使用Bing Maps Silverlight Control加载自己部署的Google Maps 上个月微软必应地图(Bing Maps) ...