博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 219:Contains Duplicate II
阅读量:4150 次
发布时间:2019-05-25

本文共 758 字,大约阅读时间需要 2 分钟。

Given an array of integers and an integer k, find out whether there 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.

此题在上一题Contains Duplicate的基础上,要求重复元素的半径不大于K。上题中,map中元素值为Key,元素索引下表为Value。所以在找到重复的情况下,可以判断下标差是否小于等于K。如果大于K,则修改已在map中元素对应的value,即下标。

class Solution {public:    bool containsNearbyDuplicate(vector
& nums, int k) { map
int_map; map
::iterator iter; for (int i = 0; i
second <= k) { return true; }else{ int_map.erase(iter); } } int_map.insert(pair
(nums[i], i)); } return false; }};

转载地址:http://rfxti.baihongyu.com/

你可能感兴趣的文章
android系统提供的常用命令行工具
查看>>
【Python基础1】变量和字符串定义
查看>>
【Python基础2】python字符串方法及格式设置
查看>>
【Python】random生成随机数
查看>>
【Python基础3】数字类型与常用运算
查看>>
Jenkins迁移jobs
查看>>
【Python基础4】for循环、while循环与if分支
查看>>
【Python基础5】列表和元组
查看>>
【Python基础6】格式化字符串
查看>>
【Python基础7】字典
查看>>
【Python基础8】函数参数
查看>>
【Python基础9】浅谈深浅拷贝及变量赋值
查看>>
Jenkins定制一个具有筛选功能的列表视图
查看>>
【Python基础10】探索模块
查看>>
【Python】将txt文件转换为html
查看>>
[Linux]Shell脚本实现按照模块信息拆分文件内容
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
在C++中,关键字explicit有什么作用
查看>>
C++中异常的处理方法以及使用了哪些关键字
查看>>