LeetCode(43)-Contains Duplicate II

时间:2023-02-22 19:24:30

题目:

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. &lbrack;LeetCode&rsqb; 219&period; 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. &lbrack;LeetCode&rsqb; 219&period; Contains Duplicate II &star;&lpar;存在重复元素2&rpar;

    每天一算: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&period; 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. &lpar;easy&rpar;LeetCode 219&period;Contains Duplicate II

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

  9. Java &lbrack;Leetcode 219&rsqb;Contains Duplicate II

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

随机推荐

  1. gridView RowDataBound事件 鼠标经过行颜色变化及根据字段值显示指定内容

    protected void gvBarInfo_RowDataBound(object sender, GridViewRowEventArgs e)        {            if ...

  2. Hibernate用注解实现实体类和表的映射

    数据库mysql: 1.一对一 person50表password50表是一对一的关系: password50表中有外键 person_id person实体类: package com.c50.en ...

  3. ASP&period;NET MVC统一异常处理

    前言: 今早看了篇文章:求知成瘾,却无作品 的思考:很有感触,发现原来自己也是这样,对每样东西都抱有很大的兴趣或者希望自己去学,一年后发现原来自己什么都是皮毛什么都不精!最终发现真正的大牛都是在某一个 ...

  4. Ext&period;Date 方法

    1.Ext.Date.add(date,interval,value); 提供执行基本日期运算的简便方法; date 日期对象, interval 一个有效的日期间隔枚举值, value 向当前日期上 ...

  5. 如何实现SQL事务的提交,又不对外进行污染

    一.以下是本人的一点思路: 1.在事务方法中,参数运用委托Func,选用Func 的原因是多入参,单一出参2.事务传参运用泛型,选用泛型的原因是可以减少代码量,类型安全 二.说明中涉及4个类:1.Or ...

  6. Codeblock解决注释乱码问题及在ubuntu中程序运行时乱码问题。

    (1)修改源文件保存编码在:settings->Editor->gernal settings>other settings 看到左边的Encoding group Box,改为WI ...

  7. Matlab spline

    请记住,,平稳 早期project图时,把富有弹性的细长木条(所谓样条)用压铁固定在样点上,在其它地方让它*弯曲,然后沿木条画下曲线. 成为样条曲线 三次样条插值(简称Spline插值)是通过一系列 ...

  8. 内存管理单元--MMU

    现代操作系统普遍采用虚拟内存管理(Virtual Memory Management)机制,这需要处理器中的MMU(Memory Management Unit,内存管理单元)提供支持,本节简要介绍M ...

  9. 今天给大家补充一下 background 用法

    补充一个知识点 1,浏览器默认字体大小是font-size:16px:谷歌最小字体是10px,其他浏览器是12px 2. 选择器 通配符选择器     *   表示 3.background  背景 ...

  10. 【转】Git超实用总结,再也不怕记忆力不好了

    [转]Git超实用总结,再也不怕记忆力不好了 欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯工蜂发表于云+社区专栏 Git 是什么? Git 是一个分布式的代码管理容器,本地和 ...