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 ...
随机推荐
- Visual studio 2008 的语法高亮插件 WordLight
前段时间一直在使用matlab,今天需要使用vs2008,而用惯了matlab,习惯了其中一项选中变量高亮的设置,突然回来使用VS,感到各种不适应,顿时想到了一个词:矫情 呵呵,于是在网上找各种插件, ...
- 使用Post方法模拟登陆爬取网页(转)
使用Post方法模拟登陆爬取网页 最近弄爬虫,遇到的一个问题就是如何使用post方法模拟登陆爬取网页.下面是极简版的代码: import java.io.BufferedReader; impor ...
- Google Kaptcha验证码的使用
原文:http://www.kailing.pub/article/index/arcid/92.html Kaptcha是什么? kaptcha 是谷歌开源的非常实用的验证码生成工具,基于Simpl ...
- vuex 存值 及 取值 的操作
1.传值 // 定义参数 let params = { workItemId: workItemId, flowInstId: flowInstId, itemStatus: itemStatus, ...
- HDU-3295-An interesting mobile game(BFS+DFS)
Problem Description XQ,one of the three Sailormoon girls,is usually playing mobile games on the clas ...
- HDU2577 How to Type【DP】
题目链接: pid=2577">http://acm.hdu.edu.cn/showproblem.php? pid=2577 题目大意: 给你一个仅仅包括大写和小写字母的字符串,如今 ...
- hadoop(九) - hbase shell命令及Java接口
一. shell命令 1. 进入hbase命令行 ./hbase shell 2. 显示hbase中的表 list 3. 创建user表,包括info.data两个列族 create 'user' ...
- Spring和SpringMVC的关系
1.Spring和SpringMVC是父子容器关系. 2.Spring整体框架的核心思想是容器,用来管理bean的生命周期,而一个项目中会包含很多容器,并且它们分上下层关系,目前最常用的一个场景是在一 ...
- b-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='") SELECT id FROM qqzoneshuoshuo WHERE words=
db-is-in- (1267, "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci, ...
- HDU 5855Less Time, More profit
Less Time, More profit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...