[leetcode] 219. Contains Duplicate II

网友投稿 1031 2022-10-01

[leetcode] 219. Contains Duplicate II

[leetcode] 219. Contains Duplicate II

Description

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 absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2Output: false

分析

题目的意思是:给你一个数组,然后给定一个k,问数组里面是否存在两个一样的数,并且间隔最大为k。

没想到用一个hash表存储每个值的索引,然后在遍历的时候判断一下间隔就行了。

代码

class Solution {public: bool containsNearbyDuplicate(vector& nums, int k) { int n=nums.size(); unordered_map m; for(int i=0;i

参考文献

​​[LeetCode] Contains Duplicate II 包含重复值之二​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信公众号菜单怎么添加很多文章?
下一篇:微信小程序看不了广告怎么办?(微信小程序看不了广告怎么办苹果)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~