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 ...
随机推荐
- postgresql进程及内存结
一.进程和内存架构图 postgresql数据库启动时,先启动一个postmaster的主进程,然后fork出一些辅助子进程. 二.主进程postmaster -bash-4.2$ which pos ...
- 复刻smartbits的国产网络性能测试工具minismb-如何配置Ping报文
复刻smartbits的国产网络性能测试工具minismb,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太网测试工具测试任何ip网络设备的端口吞吐率,带宽, ...
- ELK常用命令
1.查询当前所有的索引 #curl 'localhost:9200/_cat/indices?v' 2.查看集群健康状态 #curl 'localhost:9200/_cat/health?v' 绿色 ...
- ThreadLocal剧集(一)
总述 最近做了一个日志调用链路跟踪的项目,涉及到操作标识在线程和子线程,线程池以及远程调用之间的传递问题.最终采用了阿里开源的TransmittableThreadLocal插件(https: ...
- idea调试springmvc出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
idea调试springmvc出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderList ...
- linux下开启、关闭、重启mysql服务
linux下开启.关闭.重启mysql服务命令 一. 启动1.使用 service 启动:service mysql start2.使用 mysqld 脚本启动:/etc/inint.d/mysql ...
- [转]Centos系统中查看文件和文件夹大小
本文转自:https://blog.csdn.net/zgmu/article/details/52882868 当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择.df可以 ...
- Nodejs学习事件模块
1.nodejs 版本为v6.2.0,events是node.js 最重要的模块,events模块只提供了一个对象EventEmitter,EventEmitter 的核心是事件发射与事件监听器.可以 ...
- C# 数组深拷贝
数组深拷贝,即完全复制出一份新的数组,两个数组内容完全相同. 一般有四种方法: 1. 循环遍历复制 2. 数组的成员方法:CopyTo CopyTo方法用作将源数组全部拷贝到目标数组中,可以指定目标数 ...
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...