原文题目:

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. Servlet(API)生命周期

    一.最上层接口Servlet 查看Servlet接口源码: 有5个方法 访问过程(默认): 1.进行Servlet类加载 当Tomcat容器启动后,服务器寻找应用部署的描述文件(web.xml),从部 ...

  2. DOM内容操作和自定义、样式改变

    自定义 function 方法名或函数名(参数1,参数2,...) { 方法体: return返回值:(可不写) } function abc() { alert("123"); ...

  3. 网络基础和python

    ·五层协议 物理层,数据链路层,网络层,传输层,应用层 ·用户上网流程 1.本机获取 2.打开浏览器,,输入网址. 3.dns协议(基于udp协议) 4.HTTP部分的内容 5 TCP协议 6 IP协 ...

  4. tomcat8+memcached session共享

    一.环境准备 时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区) 关闭防火墙 软件包和jar包链接:https://pan.baidu.com/s/1sl9Nob7 二.安装配置ngi ...

  5. Anatomy of a Program in Memory.剖析程序的内存布局

    原文标题:Anatomy of a Program in Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译 ...

  6. str 操作

    str 认识字符串(重点, 多) 字符: 单一的文字符号 字符按照固定的顺序连成串 被' 或者" 或者''' 或者"""括起来的内容 索引 编号, 顺序 从0开 ...

  7. python-对象方法、静态方法、类方法

    #-*- coding:utf-8 -*- #本次学习:对象方法.静态方法.类方法 class SeniorTestingEngineer: #属性--只能对象来调用self.salary work_ ...

  8. Shell 格式化输出数字、字符串(printf)

    1.语法 printf打印格式字符串,解释'%'指令和'\'转义. 1.1.转义 printf使用时需要指定输出格式,输出后不换行. printf FORMAT [ARGUMENT] printf O ...

  9. ubuntu上virtualbox无法找到usb设备【解决】

    How to set up USB for Virtualbox? USB in different versions of Virtual Box For use of USB in Virtual ...

  10. smbpasswd 和 pdbedit 的区别

    以前我们在windows上共享文件的话,只需右击要共享的文件夹然后选择共享相关的选项设置即可.然而如何实现windows和linux的文件共享呢?这就涉及到了samba服务了,这个软件配置起来也不难, ...