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 ...
随机推荐
- hibernate dynamic-update="true"属性不起作用原因(转载)
原文地址: https://yan-sa.iteye.com/blog/1913684 由于我在action层使用了注解多例@Scope("prototype"),而在dao层默认 ...
- Android: Mac无法找到Android SDK问题
通过brew cask install android-sdk后,Intellij Idea中设置Android SDK路径失败,解决方法如下: /usr/local/Caskroom/android ...
- 实战c++中的vector系列--emplace_back造成的引用失效
上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及.今天就谈一谈emplace_back造成的引用失效. 直接撸代码了: #inclu ...
- Excel数据字典转换为PDM(且显示表名、字段相应的中文描写叙述)
在工作中遇到了一个问题就是把Excel数据字典转换为PDM. 可是转换完毕了全是英文,原来对表名.字段名的中文描写叙述就没有了. 且对于这个问题在powerdesigner15.2以后能够直接完毕.可 ...
- 12.解决CCScale9Sprite或者CCControlButton无法使用的问题。
问题: 使用CCScale9Sprite或者CCControlButton等控件的时候,会出现无法识别的情况. 解决方式: 1.include对应的头部,即#include "cocos-e ...
- S5P4418裸机开发系列教程--源代码下载
S5P4418裸机系列教程之stdio S5P4418裸机系列教程之shell命令行 S5P4418裸机系列教程之串口回显 S5P4418裸机系列教程之复位測试 S5P4418裸机系列教程之led跑马 ...
- (四)Java 基础语法
Java 基础语法 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一 ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...
- ppp点对点协议
直接链接两个信令点的一组链路称作什么? PPP点到点连接: 点对点协议(PPP)为在点对点连接上传输多协议数据包提供了一个标准方法.PPP 最初设计是为两个对等节点之间的 IP 流量传输提供一种封装协 ...
- 常用经典SQL语句大全完整版--详解+实例 《来自网络,很全没整理,寄存与此》
常用经典SQL语句大全完整版--详解+实例 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML ...