lc 217 Contains Duplicate


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的更多相关文章

  1. 25. leetcode 217. Contains Duplicate

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  2. 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 ...

  3. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  4. LeetCode 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. leetcode 217 Contains Duplicate 数组中是否有重复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  6. leetcode 217 Contains Duplicate 数组中是否有反复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  7. Java for LeetCode 217 Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. LeetCode 217 Contains Duplicate

    Problem: Given an array of integers, find if the array contains any duplicates. Your function should ...

  9. (easy)LeetCode 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

随机推荐

  1. 【SQL Server 学习系列】-- 随机生成日期时间的SQL脚本

    DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT SET @dt1='2010-01-01'--开始日期 SET @dt2='2010-0 ...

  2. Sqlserver数据库发送邮件

    目录 1. Sqlserver数据库发送邮件 1.1. 概念了解 1.2. 配置 1.3. 测试发送邮件 1.3.1. 代码测试 1.3.2. 工具测试 1.4. 查看邮件日志 1. Sqlserve ...

  3. 使用百度网盘实现自动备份VPS

    http://ju.outofmemory.cn/entry/51536 经过轰轰烈烈的一轮网盘大战,百度网盘的容量已经接近无限(比如我的是3000多G ),而且百度网盘已经开放API,所以用来备份V ...

  4. [React] Spread Component Props in JSX with React

    You often find duplication between the name of a prop and a variable you will assign to the prop. JS ...

  5. Office 针式打印机如何调节边距

    1 右击针式打印机,选择"打印机属性"   2 点击"打印机参数设置"选项卡,之前打印出来如果发现上下距离不合适,可以通过调节但也纸页顶距来调整   该参数值可 ...

  6. 使用OpenCV读、操作、写图像并与bash合作对某个文件夹下全部图像进行相似处理

    我门要对某个文件夹下全部图像文件进行统一处理,假设图像的数量过多.那么手动地一张张处理就会显得有些麻烦.本文使用OpenCV和bash来完毕我们指定的任务. 任务 将文件夹A下的全部统一格式的jpg图 ...

  7. webpack—入门

    点击进入webpack官网.,开始教程时,建议先学习ES6语法,也请先观看本篇Windows符号介绍文章,当所有webpack内容学习完后,会有一个专门的介绍 webpack四个核心概念(从官网入门— ...

  8. 【SSO】--单点登录之过滤器(filter)

    在单点登录的探索中.用到一个知识点:过滤器(filter).常见的几种验证:Authorization filters,验证用户是否有权限訪问页面:Action Filter,验证用户登录的时候是否用 ...

  9. Mariadb 主从

    一 mariadb主从多用于网站架构,因为该主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构 ...

  10. String类的四个默认成员函数

    优化版的拷贝构造函数,先创建一个暂时实例tmp,接着把tmp._ptr和this->_ptr交换,因为tmp是一个局部变量.程序执行到该函数作用域外,就会自己主动调用析构函数.释放tmp._pt ...