原文题目:

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. ​游戏设计思考:对COK的理解和思考

    转自:http://www.gameres.com/804983.html 一.前言 发此文的起因是最近加入了一个游戏研究群,受到大家对游戏研究热情的感染,也想将自己对游戏的理解和感悟发出来和大家一起 ...

  2. bootstrap样式

    图片: <img src="w.jpg" alt="" class="img-rounded"><img src=&quo ...

  3. 第11章 拾遗3:虚拟局域网(VLAN)

    1. 虚拟局域网(VLAN) (1)VLAN是建立在物理网络基础上的一种逻辑子网,它将把一个LAN划分成多个逻辑的局域网(VLAN),每个VLAN是一个广播域,VLAN内的主机间通信就和在一个LAN内 ...

  4. 如何使用webpack打包项目

    webpack是前端开发中比较常用的打包工具之一,另外还有gulp,grunt.之前没有涉及过打包这块,这里介绍一下使用webpack打包的流程. Grunt和Gulp的工作方式是:在一个配置文件中, ...

  5. C#语言经典例题

    两个例题分别用了两种不同的写入方式 一个是有Console.Write(); 一个没有,两种都可以 标准体重 男士体重 = 身高 - 100 +-3 kg cm 女士体重 = 身高 - 110 +-3 ...

  6. (转)日期类型的input元素设置默认值为当天

    原文地址 html5的form元素对日期时间有丰富的支持 <input type="date"> <input type="time"> ...

  7. [Android]Android布局优化之<include />

    转载请标明:转载于http://www.cnblogs.com/Liuyt-61/p/6602891.html -------------------------------------------- ...

  8. openStack queens

  9. exe加载DLL的时候会有一系列的搜索路径

    假如安全DLL搜索模式启用,搜索顺序如下: 1. 应用程序所在的路径 2. Windows SYSTEM目录.通过调用GetSystemDirectory函数可以获取这个目录的路径. 3. 16位系统 ...

  10. vs2008发布项目失败的解决方法

    解决办法: 要知道发布是怎么失败的,用组合键"Ctrl+Alt+O"即可,仔细查看信息可发现有没发布成功的详细提示,然后在资源管理器中找到那一项,删除或排除到项目外,重新生成之后再 ...