原文题目:

217. Contains Duplicate

219. Contains Duplicate II

读题:

217只要找出是否有重复值,

219找出重复值,且要判断两者索引之差是否小于k

//217. Contains Duplicate 46ms
class Solution
{
public:
bool containsDuplicate(vector<int>& nums)
{
set <int> s;
//vector <int>::iterator it;
int i = 0;
if(nums.empty())
{
return false;
}
s.insert(nums[0]);
for(i = 1;i < nums.size();++i)
{
if(s.count(nums[i]))
{
return true;
}
s.insert(nums[i]);
}
return false;
}
}; //217. Contains Duplicate 29ms
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
int i = 0, j = nums.size() - 1;
while (i < j) {
if (nums[i] == nums[i+1])
return true;
++i;
}
return false;
}
}; //219. Contains Duplicate II 25ms
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map <int, int> m; //如果用的是map,那么时间复杂度为35ms
for (int i = 0; i < nums.size(); ++i)
{
if (m.find(nums[i]) != m.end() && i - m[nums[i]] <= k) return true;
else m[nums[i]] = i;
}
return false;
}
};

  

217/219. Contains Duplicate /Contains Duplicate II的更多相关文章

  1. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

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

  2. [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II

    [题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...

  3. &lt;LeetCode OJ&gt; 217./219. Contains Duplicate (I / II)

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

  4. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

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

  5. Remove Duplicate Letters I & II

    Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...

  6. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  7. [LeetCode] Contains Duplicate & Contains Duplicate II

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

  8. 2017-3-11 leetcode 217 219 228

    ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...

  9. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

随机推荐

  1. CVE-2017-12615漏洞利用

    Tomcat任意文件上传漏洞CVE-2017-12615复现 今天在群里听到有人讲这个CVE-2017-12615漏洞,想起自己的虚机ubuntu里面曾经装过tomcat,午休时间来瞅瞅. 漏洞利用条 ...

  2. MongoDb进阶实践之一 如何在Linux系统上安装和配置MongoDB

    转载来源:https://www.cnblogs.com/PatrickLiu/p/8630151.html 一.NoSQL数据简介 1.NoSQL概念 NoSQL(NoSQL = Not Only ...

  3. delphi版本对应

    delphi 7 delphi 8delphi 2005 ----- 9delphi 2006 ----- 10 delphi 2007 ----- 11delphi 2009 ----- 12 de ...

  4. (转)C#.NET WINFORM应用程序中控制应用程序只启动一次

    原文地址 :http://www.cnblogs.com/emanlee/archive/2009/08/31/1557379.html using System; using System.Thre ...

  5. DB通用类:SQL Server 通用类库

    SQLServer通用类A using System; using System.Data; using System.Data.SqlClient; using System.Collections ...

  6. Linux性能优化 第三章 性能工具:系统内存

    3.1内存性能统计信息 3.1.1 内存子系统和性能 和CPU相比,内存的读写速度都大大落后于CPU.为了弥补这个差距,通常CPU会采用高速缓存的机制(高cache). 3.1.2 内存子系统(虚拟存 ...

  7. windows下mysql5.7 root密码重置

    1.在mysql根目录下新建配置文件my.ini(因为我安装的mysql-5.7.24安装完成后未看到该配置文件,所以新建一个,有的直接修改即可) [mysqld] skip-grant-tables ...

  8. kotlin的安装(一)

    1.下载Kotlin Compiler Kotlin 命令行环境主要依赖就是Kotlin Compiler,目前最新版本是 1.1.2-2.其下载链接是:https://github.com/JetB ...

  9. django之模板系统 --》内容(filter过滤器、tags标签【for、if、with】、母板以及继承、crf_token、注释、组件、静态文件【load static】、get_static_prefix、自定义标签和tag)

    常用: Django模板中只需要记两种特殊符号: {{ }}和 {% %} {{ }}表示变量,在模板渲染的时候替换成值,{% %}表示逻辑相关的操作. 变量 {{ 变量名 }} 变量名由字母数字和下 ...

  10. [Unity插件]AI行为树使用总结

    参考链接: https://blog.csdn.net/linxinfa/article/details/72937709 https://blog.csdn.net/wanghaodiablo/ar ...