题目:

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 j is at most k.

思路:

  • 给定一个数组a,和一个整数k,题意是判断一个数组里面,有没有重复的两个元素,坐标是i和j,且i和j只差大于等于k。
  • 考虑用hashMap

代码:

public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if(nums==null || nums.length<2) return false;
        //key=int, val=index
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i=0; i<nums.length; i++) {
            if(map.containsKey(nums[i])) {
                int j = map.get(nums[i]);
                if(i-j<=k) {return true;

                }else{
                    map.remove(j);
                    map.put(nums[i], i);
                }
            } else {
                map.put(nums[i], i);
            }
        }
        return false;
    }
}

LeetCode(43)-Contains Duplicate II的更多相关文章

  1. [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 ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  3. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  4. LeetCode 219. Contains Duplicate II (包含重复项之二)

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

  5. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  6. Java for LeetCode 219 Contains Duplicate II

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

  7. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  8. (easy)LeetCode 219.Contains Duplicate II

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

  9. Java [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

随机推荐

  1. iOS常见控件的基本使用

    UI相关类继承关系 UIView 常见属性和方法 UIView属性 UIView方法 UIControl 常用控件 UIImageView 图片显示控件android ImageView UISlid ...

  2. iOS开发之使用block块进行数据遍历的方法

    看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...

  3. Select标签 根据value值默认选中 Jquery

    网上找了很多都是错的,不行的. 下面方法可以的 <script type="text/javascript"> $(document).ready(function() ...

  4. IDEA阅读Spark源码

    将spark编译成idea-sbt工程 tar -zxvf spark-1.1.0.tgz cd spark-1.1.0 sbt/sbt gen-idea 等待-- 成功后就能以SBT工程的形式导入i ...

  5. linux ubuntu系统下MySQL的安装及设置

    debian下安装MySQL:1.构建源或使用光盘镜像,当然你插入光盘也没问题2.有源时本地文件的源配置:修改/etc/apt/sources.list文件, 示例:deb http://192.16 ...

  6. 基于V4L2摄像头采集图片程序设计

    #ifndef __COMMON_H #define __COMMON_H //该头文件定义的是摄像头在屏幕上显示的宽度和高度 #include<stdio.h> #include< ...

  7. Tomcat集群应用部署的实现机制

    集群应用部署是一个很重要的应用场景,设想一下如果没有集群应用部署功能,每当我们发布应用时都要登陆每台机器对每个tomcat实例进行部署,这些工作量都是繁杂且重复的,而对于进步青年的程序员来说是不能容忍 ...

  8. 2014年7月10日,我人生的最重要Upgrade

    2014年7月10日上午,我的小公主顺利的出生于国妇婴.之前各种紧张,各种不安.在不安中的前天晚上陪着来上海的董博士于方先生在人民广场聚餐.大家都是工作几年的,各种感慨,对于工作中的零零种种.还有对未 ...

  9. android值得珍藏的6个开源框架技术

    1.volley  项目地址 https://github.com/smanikandan14/Volley-demo JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的 ...

  10. [C++学习历程]基础部分 C++中的指针数组和结构

    作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/19938177 一.指针 对学习C++来说,指针是一项重要内容,以前,教 ...