32 lines
801 B
C
32 lines
801 B
C
//
|
|
// Created by xfj12 on 2025/3/24.
|
|
//
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
|
|
bool containsNearbyDuplicate(int *nums, int numsSize, int k)
|
|
{
|
|
for (int i = 0; i < numsSize; i++)
|
|
{
|
|
for (int j = i; j < numsSize; j++)
|
|
{
|
|
if (abs(i - j) > k)
|
|
continue;
|
|
if (nums[i] == nums[j] && i != j)
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int nums1[] = {1, 2, 3, 1};
|
|
assert(containsNearbyDuplicate(nums1, sizeof(nums1) / sizeof(int), 3) == true);
|
|
int nums2[] = {1, 0, 1, 1};
|
|
assert(containsNearbyDuplicate(nums2, sizeof(nums2) / sizeof(int), 1) == true);
|
|
int nums3[] = {1, 2, 3, 1, 2, 3};
|
|
assert(containsNearbyDuplicate(nums3, sizeof(nums3) / sizeof(int), 2) == false);
|
|
} |