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 numsi == numsj 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 <= numsi <= 10^9 −109<=numsi<=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;
}
相关推荐
狮子座明仔5 分钟前
DeCoRL:把推理链拆成“乐团合奏“——AAAI 2026 一篇把 RLHF 推到 32B 打 GPT-4o 的工作
人工智能·深度学习·算法
QiLinkOS6 分钟前
合肥气链科技有限公司创办与未来技术应用
c语言·数据结构·c++·人工智能·单片机·嵌入式硬件·算法
妄想出头的工业炼药师12 分钟前
追踪定位大模型
算法·开源
Solis程序员14 分钟前
TreeMap 核心原理与实战
java·数据结构·算法
Byte Wizard22 分钟前
动态内存管理
c语言·开发语言
zzzsde23 分钟前
【Linux】线程同步和互斥(5):线程池的实现&&线程安全
linux·运维·服务器·开发语言·算法·安全
weixin_4684668523 分钟前
机器学习数据预处理新手实战指南
人工智能·python·算法·机器学习·编程·数据预处理
无忧.芙桃25 分钟前
C语言文件操作
c语言·开发语言
zhangfeng113327 分钟前
glibc = GNU C Library (GNU C 标准库)CentOS 7 (glibc 2.17) pip支持
c语言·人工智能·神经网络·机器学习·centos·gnu
国科安芯32 分钟前
ASM232S电气特性与TIA/EIA-232-F及ITU V.28标准符合性深度分析
单片机·嵌入式硬件·算法·安全·架构