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;
}
相关推荐
Java成神之路-4 分钟前
【LeetCode 刷题笔记】69.x 的平方根 | 二分查找经典刷题题解
算法·leetcode
灵智实验室5 分钟前
PX4状态估计技术EKF2详解(一):EKF2 开篇——从分离到统一
算法·无人机·px 4
小智老师PMP6 分钟前
六月PMP晚启动急救|现在开始,每天2-3小时,稳冲一次上岸(附可直接照搬计划)
算法·软件工程·求职招聘·产品经理·敏捷流程
ZK_H13 分钟前
观星者手记_开发日志1
c语言
tankeven24 分钟前
动态规划专题(11):区间动态规划之三角剖分问题
c++·算法·动态规划
joshchen21536 分钟前
强化学习基础(赵世钰)第一章
人工智能·深度学习·算法·机器学习·强化学习
zhangrelay37 分钟前
三分钟云课实践速通--C/C++程序设计--
linux·c语言·c++·笔记·学习·ubuntu
小此方43 分钟前
Re:从零开始的 C++ STL篇(十二)深度解析哈希函数设计、负载因子调节与两种冲突处理策略
c++·算法·哈希算法
lcj25111 小时前
【数据结构精讲】堆与二叉树从底层原理到代码落地:堆的构建 / 调整 / 排序 + 二叉树遍历 / 操作(附完整 C++ 源码 + LeetCode 题解)
数据结构·c++·leetcode
努力努力再努力wz1 小时前
【MySQL 进阶系列】C/C++ 如何通过客户端库访问 MySQL?从连接原理到 API 调用流程详解(附完整demo代码)
服务器·c语言·数据结构·数据库·c++·b树·mysql