1、题目描述

2、题目分析

使用哈希表 和分情况讨论的方法

3、代码

bool containsNearbyDuplicate(vector<int>& nums, int k) {

        if( nums.size() ==  ){
return false;
} if( k >= nums.size() ){
map<int,int> m;
for( auto n: nums){
m[n]++;
if(m[n] > ){
return true;
}
}
return false;
} map<int,int>m;
for( int i = ; i < nums.size() - k +; i++){
if( i == ){
for(int j = i; j <= k; j++){
m[nums[j]]++;
if(m[nums[i+k]] > )
return true;
}
}else{
if( m[nums[i-]] <= ){
m.erase(nums[i-]);
}else {
m[nums[i-]]--;
}
if( i+k < nums.size() ){
m[nums[i+k]]++;
if(m[nums[i+k]] > )
return true;
}
} } return false; }

LeetCode题解之Contains Duplicate II的更多相关文章

  1. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. LeetCode算法题-Contains Duplicate II(Java实现)

    这是悦乐书的第193次更新,第197篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第53题(顺位题号是219).给定整数数组和整数k,找出数组中是否存在两个不同的索引i和 ...

  3. LeetCode 题解 | 面试题57 - II. 和为s的连续正数序列

    题目描述 面试题57 - II. 和为s的连续正数序列 难度简单37收藏分享切换为英文关注反馈 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内 ...

  4. 【LeetCode】219. Contains Duplicate II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...

  5. 【LeetCode】219. Contains Duplicate II

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

  6. LeetCode题解之Unique Paths II

    1.题目描述 2.问题描述 使用动态规划算法,加上条件检测即可 3.代码 int uniquePathsWithObstacles(vector<vector<int>>&am ...

  7. Leetcode题解之Valid Palindrome II

    1.题目描述 2.问题分析 使用两个下标,检测下标对应的字符是否相等,若不相等,则考察子串. 3.代码 bool validPalindrome(string s) { , j = s.size()- ...

  8. leetCode题解之Contains Duplicate

    1.题目描述 2.题目分析 直接使用hashTable 计数,超过1 则返回true,最后返回 false即可. 3.代码 bool containsDuplicate(vector<int&g ...

  9. [LeetCode 题解]: Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. Python Mock的入门学习

    一.Mock是什么 Mock这个词在英语中有模拟的这个意思,因此我们可以猜测出这个库的主要功能是模拟一些东西.准确的说,Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象 ...

  2. spring-boot-starter-actuator

    首先在pom中添加依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  3. markdown简单入门

    1.斜体和加粗: 使用下划线"_"或"*"括起来 _内容_ or *内容* 1个_ 或 * 都是斜体,2个则是加粗: 3个既斜体 又加粗,4个以上则没什么变化  ...

  4. android手机安全性测试手段

    罗列一下自己常用的android手机安全性测试攻击手段: 1. fiddler和tcpdump+wireshark抓包分析,模拟修改http请求参数,检验漏洞 2. 修改AndroidManifest ...

  5. git第一节----git config配置

    @查看git的版本 git --version @查看git配置信息 git config --list config list分全局和局部,在根目录下执行git config --list显示为全局 ...

  6. Spring Import注解

    今天了解了,Spring @Import的使用 先贴上Spring官方关于Spring @Import注解的文档链接   https://docs.spring.io/spring/docs/3.0. ...

  7. oracle 删除服务sc delete Oracle

    oracle 删除服务sc delete OracleVssWriterorcl -----(oracle orcl vss writer service的服务名!)

  8. (转) C# Async与Await的使用

    (转) C# Async与Await的使用 class Program { static void Main(string[] args) { Console.WriteLine("主线程测 ...

  9. Spring Boot配置Mybatis

    在pom里加了mybatis的依赖后,在application.properties加上: mybatis.config-location=classpath:mybatis-config.xml m ...

  10. Pupu(hdu3003)数论

    Pupu Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...