LeetCode题解之Contains Duplicate II
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的更多相关文章
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode算法题-Contains Duplicate II(Java实现)
这是悦乐书的第193次更新,第197篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第53题(顺位题号是219).给定整数数组和整数k,找出数组中是否存在两个不同的索引i和 ...
- LeetCode 题解 | 面试题57 - II. 和为s的连续正数序列
题目描述 面试题57 - II. 和为s的连续正数序列 难度简单37收藏分享切换为英文关注反馈 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内 ...
- 【LeetCode】219. Contains Duplicate II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用set 使用字典 日期 题目地址:https:/ ...
- 【LeetCode】219. Contains Duplicate II
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- LeetCode题解之Unique Paths II
1.题目描述 2.问题描述 使用动态规划算法,加上条件检测即可 3.代码 int uniquePathsWithObstacles(vector<vector<int>>&am ...
- Leetcode题解之Valid Palindrome II
1.题目描述 2.问题分析 使用两个下标,检测下标对应的字符是否相等,若不相等,则考察子串. 3.代码 bool validPalindrome(string s) { , j = s.size()- ...
- leetCode题解之Contains Duplicate
1.题目描述 2.题目分析 直接使用hashTable 计数,超过1 则返回true,最后返回 false即可. 3.代码 bool containsDuplicate(vector<int&g ...
- [LeetCode 题解]: Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- Windows 8.1 GetVersionEx返回6.2.9200 的问题!
http://msdn.microsoft.com/en-us/library/windows/desktop/dn302074.aspx http://tunps.com/getversionex- ...
- 轻量级web富文本框——wangEditor使用手册(3)——如何自定义配置菜单 demo
最新版wangEditor: 配置说明:http://www.wangeditor.com/doc.html demo演示:http://www.wangeditor.com/wangEditor/d ...
- tsung压力测试——安装
在安装之前确保安装了以下工具: erlang 必须要有安装java环境,要不然不成功 yum install gcc yum install gcc-c++ yum install libtool y ...
- 实现php的startsWith和endsWith
startsWith(): function startsWith($haystack, $needle){ return strncmp($haystack, $needle, strlen($ne ...
- 揭开Future的神秘面纱——结果获取
前言 在前面的两篇博文中,已经介绍利用FutureTask任务的执行流程,以及利用其实现的cancel方法取消任务的情况.本篇就来介绍下,线程任务的结果获取. 系列目录 揭开Future的神秘面纱—— ...
- mysql cmd 启动服务
1.确定你的mysql 是否能正常工作登录数据库cmd--“命令提示字符”窗口录入,录入cd C:\mysql\bin 并按下回车键,将目录切换为 cd C:\mysql\bin再键入命令mysql ...
- 小程序获取地址授权的修改 wx.openSetting需点击
开发者可以通过 wx.openSetting 接口来打开小程序设置界面并返回用户的设置结果.在原来的 wx.openSetting 接口中,我们允许开发者直接调用此接口,但目前我们发现有不少开发者滥用 ...
- Spring-web初始化流程简图
- SpringMVC源码阅读:拦截器
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...
- 并发编程之 wait notify 方法剖析
前言 2018 元旦快乐. 摘要: notify wait 如何使用? 为什么必须在同步块中? 使用 notify wait 实现一个简单的生产者消费者模型 底层实现原理 1. notify wait ...