LeetCode //C - 219. Contains Duplicate II

219. Contains Duplicate II

Given an integer array nums and an integer k , return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k .

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

  • 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
  • − 1 0 9 < = n u m s [ i ] < = 1 0 9 -10^9 <= nums[i] <= 10^9 −109<=nums[i]<=109
  • 0 < = k < = 1 0 5 0 <= k <= 10^5 0<=k<=105

From: LeetCode

Link: 219. Contains Duplicate II


Solution:

Ideas:
  1. Structure Definition: A structure Number is defined to store two integers: the value of an element from the nums array and its original index in the array.

  2. Array Initialization: An array numbers of type Number is created and initialized with the values and corresponding indices from the input array nums.

  3. Sorting: The numbers array is sorted by the values using the qsort function. The compare function is used to define the sorting order, which is based on the value field of the structure. After sorting, elements with the same value will be adjacent to each other, and their original indices can still be accessed.

  4. Scanning for Duplicates: The sorted numbers array is then scanned to find adjacent pairs with the same value. If such a pair is found, the original indices are checked to see if the absolute difference between them is less than or equal to k. If this condition is met, the function returns true.

  5. Memory Cleanup: The dynamically allocated memory for the numbers array is freed before exiting the function.

  6. Result: If no such pair is found that meets the condition, the function returns false.

By sorting the numbers and keeping track of the original indices, this approach efficiently checks for duplicates within the specified range k. The sorting ensures that the same values are grouped together, making it easy to check the condition without needing a hash table.

Code:
c 复制代码
typedef struct {
    int value;
    int index;
} Number;

int compare(const void *a, const void *b) {
    return ((Number*)a)->value - ((Number*)b)->value;
}

bool containsNearbyDuplicate(int* nums, int numsSize, int k) {
    if (k == 0 || numsSize <= 1) return false;
    
    Number* numbers = (Number*)malloc(numsSize * sizeof(Number));
    for (int i = 0; i < numsSize; i++) {
        numbers[i].value = nums[i];
        numbers[i].index = i;
    }
    
    qsort(numbers, numsSize, sizeof(Number), compare);
    
    for (int i = 0; i < numsSize - 1; i++) {
        if (numbers[i].value == numbers[i + 1].value && 
            abs(numbers[i].index - numbers[i + 1].index) <= k) {
            free(numbers);
            return true;
        }
    }
    
    free(numbers);
    return false;
}
相关推荐
炸膛坦客几秒前
嵌入式 - 数据结构与算法:(1-9)数据结构 - 队列(Queue)
c语言·数据结构
~|Bernard|27 分钟前
二.go语言中map的底层原理(2026-5-8)
算法·golang·哈希算法
mask哥40 分钟前
力扣算法java实现汇总整理(下)
java·算法·leetcode
代码中介商41 分钟前
栈结构完全指南:顺序栈实现精讲
c语言·开发语言·数据结构
样例过了就是过了1 小时前
LeetCode热题100 编辑距离
数据结构·c++·算法·leetcode·动态规划
wearegogog1231 小时前
MATLAB椭圆参数检测算法实现
数据库·算法·matlab
secondyoung1 小时前
Markdown数学公式语法速查手册
算法·编辑器·markdown·latex
君义_noip1 小时前
CSP-S 2025 提高级 第一轮(初赛) 阅读程序(1)
算法·深度优先·信息学奥赛·初赛
小O的算法实验室1 小时前
2026年IEEE TEVC,知识引导的竞争进化算法用于多解传感器-武器-目标分配问题,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
khalil10202 小时前
代码随想录算法训练营Day-46 动态规划13 | 647. 回文子串、516.最长回文子序列、动态规划总结
数据结构·c++·算法·leetcode·动态规划·回文子串·回文子序列