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

这题上上一篇博客的延伸,问的是k长的距离内有没有两个数是相等的,类似一个滑动窗口问题,方法比较简单,使用一个map记下上次数出现的位置就可以了,代码如下:

 class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> ret;
int sz = nums.size();
for (int i = ; i < sz; ++i){
if (ret.find(nums[i]) != ret.end() && i - ret[nums[i]] <= k)
return true;
else
ret[nums[i]] = i;
}
return false;
}
};

java版本的代码如下所示,用的方法都是一样的:

  public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; ++i){
if(m.containsKey(nums[i]))
if(i-m.get(nums[i]) <= k)
return true;
m.put(nums[i], i);//放在这里有两个原因,如果本来存在将index更新到最近的位置,如果不存在就将它放到map中起
}
return false;
}
}

LeetCode OJ:Contains DuplicateII(是否包含重复II)的更多相关文章

  1. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  2. LeetCode 217. Contains Duplicate (包含重复项)

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

  3. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. LeetCode OJ 95. Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  5. &lt;LeetCode OJ&gt; 78 / 90 Subsets (I / II)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  6. LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  8. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

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

  9. [LeetCode] 217. Contains Duplicate 包含重复元素

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

随机推荐

  1. 74LS85 比較器 【数字电路】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/27959219 74LS85 demo: 11 ...

  2. Android Studio的快捷键

    Android Studio可以在setting的keymaps设置快捷键,但最好使用该默认的快捷键. 生成TAG: logt 控制台打印带参的log:logm 代码提示:ctrl + alt + s ...

  3. 【Zookeeper】初识zookeeper

    单机模式 安装并解压: 修改配置文件,conf/zoo.cfg(配置完成后,启动后,可以通过netstat-ano命令查看是否有你配置的clientPort端口号在监听服务) tickTime: zo ...

  4. 两张Excel表比较,两个for循环比较优化使用Contains

    将一个Excel表中的sheet中的一列导出到List<string>,用一个for循环循环另一张表中的数据,看是否在第一张表中的Contains中

  5. 在像Angular2这样的SPA应用中使用Google Analytics的方法

    Angular2のようなシングルページアプリケーションでGoogleアナリティクスを使う方法 如何在像Angular2这样的SPA应用中使用Google Analytics? 试着调查了一下. 由于S ...

  6. 【转】Python的hasattr() getattr() setattr() 函数使用方法详解

    Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...

  7. Django---media静态文件的配置&全局变量

    media 静态文件配置 static 静态文件多用于存放用于渲染前端页面的相关数据,media用于存放客户上传或其他的文件 setting.py 中加入路径 MEDIA_ROOT = ( os.pa ...

  8. php多个数组同键名键值相加合并

    //任意多个相同键值的数组合并相加 //预先将所要合并的数组组装成一个新的数组 // $arr = array( // array( // 'user_id' => 100, // 'goods ...

  9. JAVA 集成 Ueditor 百度富文本编辑器

    开发环境:一个简单的SpringMVC框架中,用百度富文本编辑器 ueditor 实现图片和文件的上传 官网地址:http://ueditor.baidu.com/website/ 需要使用到的2个文 ...

  10. cuffdiff problem--Performed 0 isoform-level transcription difference tests

    如何解决cuffdiff运行结果中表达量为0的情况? cuffdiff -o cdiffout -b ref.fasta -u -p 15 --library-type fr-firststrand ...