Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

Have you met this question in a real interview?
 
方法一:
分析:可以直接定义一个长度最大为k的滑动窗口,用一个set维护窗口内的数字判断是否出现重复,使用两个指针start和end标记滑动窗口的两端,初始都是0,然后end不断进行扩展,扫描元素判断是否出现重复元素,直到发现end-start>k, 就开始移动start,并且在set中移除对应的元素。如果以为扫描到数组末尾还没有发现重复元素,那就可以返回false。时间复杂度和空间复杂度都是 O(N)。
 class Solution
{
public:
bool containsNearbyDuplicate(vector<int> &nums, int k)
{
set<int> S;
int start = , end = ; for(int i=; i<nums.size(); i++)
{
if(S.find(nums[i]) != S.end())
return true;
else
{
S.insert(nums[i]);
end++;
} if(end-start > k)
{
S.erase(nums[start]);
start++;
}
} return false;
}
};
 
方法二:
使用map记录出现过的nums[i],并记录对应的位置i,当出现冲突时,比较两者位置关系,如果小于等于k,则返回true,否则记录新位置;如果没有冲突,则说明不含有重复元素。
 class Solution
{
public:
bool containsNearbyDuplicate(vector<int> &nums, int k)
{
map<int, int> M; for(int i=; 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;
}
};

[leetcode] Contains Duplicate II的更多相关文章

  1. [LeetCode] Contains Duplicate(II,III)

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

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

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

  3. LeetCode Contains Duplicate II (判断重复元素)

    题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...

  4. leetcode Contains Duplicate II python

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  5. LeetCode & Q219-Contains Duplicate II

    Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...

  6. LeetCode——Contains Duplicate II

    Description: Given an array of integers and an integer k, find out whether there there are two disti ...

  7. leetcode:Contains Duplicate和Contains Duplicate II

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

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

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

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

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

随机推荐

  1. 转载:更换zImage中的initramfs

    From: http://blog.csdn.net/linuxaxis/article/details/8769722 好吧,折腾了两三个星期,USB的问题没搞定,看来功夫还不到家,看了下efuse ...

  2. 背影渐变shape写法

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  3. ruby -- 问题解决(七)ActionController::InvalidAuthenticityToken解决办法

    学习链接:http://cnkerry.iteye.com/blog/350718 解决方法一: class FooController < ApplicationController prot ...

  4. thinkphp -- 解决连接mssql后台管理菜单显示中文乱码问题(备忘)

    一开始使用的是mysql,数据库的编码是UTF-8 后来换数据库,mysql换成mssql2005,数据库编码为GBK,管理菜单出现乱码,如下所示(左图正常,右图乱码) 解决方法如下: 第一,查看数据 ...

  5. [OpenCV] Image Processing - Image Elementary Knowledge

    "没有坚实的理论基础,实践只会浅尝于表面." 这是两本打基础的书,没系统学过的话,怎么好意思说自己会CV. 该领域,兴军亮 这个名字屡次出现,看来是计算机视觉领域国内的年轻才俊,向 ...

  6. 五分钟,运用cocoaui库,搭建主流iOS app中我的界面

    本项目基于天天团购项目,在上一篇中有说到! 首先介绍一些cocoaui,是国内的一名程序员做的开源的开源系统,目的是为了简化ios布局!官网地址:www.cocoaui.com,github地址:ht ...

  7. CSS魔法堂:Position定位详解

    一.Position各属性值详解   1.  static :默认值,元素将按照正常文档流规则排列.   2.  relative :相对定位,元素仍然处于正常文档流当中,但可以通过left.top. ...

  8. Windows平台下ActiveMQ 安装

    安装之前需要先确定机器上已经有JVM环境,如果没有则会在安装过程中提示 Unable to execute Java command.  系统找不到指定的文件 第一步:从官网下载ActiveMQ的安装 ...

  9. JavaScript 中变量、作用域和内存问题的学习

    这是我学习JavaScript的第二篇文章,之前做过几年的Java开发,发现JavaScript虽然也是面向对象的语言但是确实有很多不同之处.就本篇博客,主要学习总结一下最近学习到的JavaScrip ...

  10. [ORM] Entity Framework(1) CodeFirst快速入门

    Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案 对象关系映射(英语:Object Relational Mapping ...