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;
}
相关推荐
集思广益的灰太狼2 分钟前
变频器启动致PLC数据异常?西门子G120配合滤波器EMC抑制方案
人工智能·算法·工控·emc·电磁兼容·变频器·西门子
zbyyd5 分钟前
Linux 进程间通信(IPC):信号、消息队列、共享内存与信号量详解
linux·运维·c语言
方方洛18 分钟前
vllm教程-03-架构设计
算法·架构
玄昌盛不会编程20 分钟前
LeetCode——2091. 从数组中移除最大值和最小值
java·算法·leetcode
如何取名32 分钟前
结合实际业务进行LRU淘汰算法优化设计(数据库开发日志)
数据库·算法
aichitang202437 分钟前
快乐泛函每一天:希尔伯特空间中的最佳逼近与正交投影定理
人工智能·考研·算法·ai·面试·泛函分析
淬炼之火41 分钟前
笔记:Visually-Guided Policy Optimization for Multimodal Reasoning
人工智能·笔记·算法·机器学习·语言模型·自然语言处理
一只QAQ43 分钟前
c++项目
java·c++·算法
学习星球1 小时前
空天地一体化网络(NTN)深度解析:从Starlink D2C到3GPP NTN,卫星直连手机是如何实现的?
网络·人工智能·算法·智能手机·php
科技林总1 小时前
提示词测评落地全流程
人工智能·算法