美文网首页
Contains Duplicate II

Contains Duplicate II

作者: aemaeth | 来源:发表于2016-03-14 10:24 被阅读0次

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

Note:
Like sliding window protocol.

bool containsNearbyDuplicate(int* nums, int numsSize, int k) { if(numsSize<=1) return false; for(int i=0;i<numsSize;i++) for(int j=i+1;j<=i+k&&j<numsSize;j++){ if(nums[i]==nums[j]) return true; } return false; }

相关文章

网友评论

      本文标题:Contains Duplicate II

      本文链接:https://www.haomeiwen.com/subject/bijflttx.html