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; }
网友评论