一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个数组,如果数组中存在连个相等的数之间的距离小于k,则返回true,反之返回false。

解题思路:只需要找到距离小于K的两个相等的数,很容易想到采用hash表的算法。

定义一个hashmap,hashmap[i]表示i这个数最近出现的位置。

当遍历到下一个i出现时,计算i-hashmap[i],如果小于k则代表存在,反之则不存在。

具体解释看代码:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int,int>  hash;//STL中的hashmap为unordered_map
        int size = nums.size();
        for(int i = 0 ; i < size ; i++){
            if(hash.find(nums[i])!=hash.end()){//如果之前出现过nums[i]这个数,就计算距离
                if(i - hash[nums[i]]<=k) return true;
            }
            hash[nums[i]] = i;//保存最近出现nums[i]的位置
        }
        return false;
    }
};

【一天一道LeetCode】#219. 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. 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 ...

  4. LeetCode 219 Contains Duplicate II

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

  5. 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 ...

  6. Leetcode 219 Contains Duplicate II STL

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

  7. (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 ...

  8. Java [Leetcode 219]Contains Duplicate II

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

  9. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

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

随机推荐

  1. BZOJ4870: [Shoi2017]组合数问题

    4870: [Shoi2017]组合数问题 Description Input 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ...

  2. Mysql锁机制--写锁

    Mysql 系列文章主页 =============== 1 准备数据 1.1 建表 1.1.1 建立 Employee表 DROP TABLE IF EXISTS employee; CREATE ...

  3. 解决com.fasterxml.jackson.databind.JsonMappingException: No suitable

    原因:直接翻译,json格式,不匹配. 这原因坑爹啊,因为json格式不正确算一种原因. 还有一种就是接收的bean没有getter,setter方法. 最坑的一种就是数据无法被反序列化,list,m ...

  4. NPOI给单元格加范围边框

    HSSFWorkbook workbook2 = new HSSFWorkbook();        //XSSFWorkbook workbook2 = new XSSFWorkbook();// ...

  5. sololearn的c++学习记录_4m11d

    Function Templates Functions and classes help to make programs easier to write, safer, and more main ...

  6. MFC回车事件

    这是一个使用MFC开发关于设备控制的windows应用程序 通过该项目我学到的内容: 继承的好处 应用程序的界面是与应用程序的代码有一定的对应关系的,界面中不同的控件对应不同的类,首先就是需要一个对话 ...

  7. grpc的服务注册与发现及负载

    参考文章: (1)https://segmentfault.com/a/1190000008672912 (2)https://grpc.io/docs/ (3)https://github.com/ ...

  8. 使用FFMPEG在windows平台下推rtmp流

    使用FFMPEG在windows平台下推rtmp流 工作中习惯在Linux下面使用FFmpeg模拟推rtmp流,无奈家中的电脑都是windows系统,需要利用家中的带宽来测试流媒体服务器的性能.所以研 ...

  9. JavaScript 流程语句知识脑图

  10. Java对象的访问定位

    java对象在访问的时候,我们需要通过java虚拟机栈的reference类型的数据去操作具体的对象.由于reference类型在java虚拟机规范中只规定了一个对象的引用,并没有定义这个这个引用应该 ...