[leetcode-219-Contains Duplicate II]
Posted hellowOOOrld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-219-Contains Duplicate II]相关的知识,希望对你有一定的参考价值。
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.
思路:
用一个map,记录关键字和下标。
bool containsNearbyDuplicate(vector<int>& nums, int k) { if(nums.size()<=1 || k<=0) return false; map<int,int>m; for(int i=0;i<nums.size();i++) { if(m.count(nums[i]) && abs(m[nums[i]]-i)<=k) { return true; } else { m[nums[i]] = i; } } return false; }
以上是关于[leetcode-219-Contains Duplicate II]的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 219 Contains Duplicate II
[leetcode-219-Contains Duplicate II]
leetcode219 Contains Duplicate2
Java [Leetcode 219]Contains Duplicate II