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. JS中style属性

    解决办法:1.先定义一个CSS规则,然后this.className=''2.document.getElementByIdx_x("a").style.cssText=" ...

  2. HIVE: SerDe应用实例

    数据文件内容 id=123,name=steven id=55,name=ray 期望输出格式 123 steven 55 ray 1. 创建表, 用正则表达式的形式指定格式 create table ...

  3. Powerdesigner逆向工程从sql server数据库生成pdm (转载)

    第一步:打开"控制面板"中的"管理工具" 第二步:点击"管理工具"然后双击"数据源(odbc)" 第三步:打开之后,点击 ...

  4. C#获取类以及类下的方法(用于Asp.Net MVC)

    在C#中,实现动态获取类和方法主要通过反射来实现,要引用System.Reflection. public ActionResult GetControllerAndAction() List< ...

  5. storm分组模式

    Shuffle grouping: Tuples被随机分配到每一个bolt’s task,以便于每一个bolt’s task获得相同数量的tuples. Fields grouping: Stream ...

  6. 2014 网选 5014 Number Sequence(异或)

    /* 题意:a, b两个序列,规定由[0, n]区间的数! 求 a[i] ^ b[i] 的和最大! 思路:如果数字 n的二进制有x位, 那么一定存在一个数字m,使得n^m的所有二进制位 都是1,也就是 ...

  7. LeetCode——Find the Duplicate Number

    Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...

  8. zabbix配fpmmm(mpm)数据传送不了问题解决

    我们环境用zabbix mpm来监控mysql,不过最近官网已经不叫mpm了,而是叫fpmmm,理由为: fpmmm is the successor of mpm. mpm was renamed ...

  9. Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)

    Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...

  10. 11.22 点餐APP第一阶段总结

    第一个冲刺结束了,任务也算是完成了. 团队合作不像单独做那样想怎么来就怎么来,各个人都不同的意见,最后方案的需要每个人一致同意通过才能执行. 不过团队合作分配到每个人的任务也相对轻一点,而且遇到问题解 ...