[LeetCode Daily Question] - 219. There are repeating elements II

One [topic category]

  • hash table

Two [question difficulty]

  • Simple

Three [topic number]

  • 219. Existence of Repeating Elements II

Four [title description]

  • Given an integer array nums and an integer k, determine whether there are two different indices i and j in the array, satisfying nums[i] == nums[j] and abs(i - j) <= k. Returns true if present; otherwise, returns false.

Five [topic examples]

  • 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

Six [topic prompt]

  • 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

Seven [problem-solving ideas]

  • The first thought of this question is the violent solution, but this solution will time out, so we thought of the hash table
  • Put the element into the hash table. The current element value is the key of the hash table, and the index subscript is val. If the same element is encountered again, we will take out the previously stored val and find the difference. If the difference is less than or equal to k, then return true
  • If there is no desired result after traversing the entire array, then return false
  • In fact, this question is essentially to maintain a recent index subscript

Eight 【Time Frequency】

  • Time complexity: O ( n ) O(n)O(n) n n n is the length of the incoming array
  • Space Complexity: O ( n ) O(n)O(n) n n n is the length of the incoming array

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public boolean containsNearbyDuplicate(int[] nums, int k) {
    
    
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0;i < nums.length;i++){
    
    
            if(map.containsKey(nums[i])){
    
    
                if(Math.abs(map.get(nums[i]) - i) <= k){
    
    
                    return true;
                }
            }
            map.put(nums[i], i);
        }
        return false;
    }
}
  1. C language version
struct HashEntry
{
    
    
    int key;
    int val;
    UT_hash_handle hh;
};

void hashAddItem(struct HashEntry **obj, int key, int val)
{
    
    
    struct HashEntry* pEntry = (struct HashEntry*)malloc(sizeof(struct HashEntry));
    pEntry->key = key;
    pEntry->val = val;
    HASH_ADD_INT(*obj, key, pEntry);
}

struct HashEntry* hashFindItem(const struct HashEntry** obj, int key)
{
    
    
    struct HashEntry* pEntry = NULL;
    HASH_FIND_INT(*obj, &key, pEntry);
    return pEntry;
}

void hashFreeAll(struct HashEntry** obj)
{
    
    
    struct HashEntry *curr, *next;
    HASH_ITER(hh, *obj, curr, next)
    {
    
    
        HASH_DEL(*obj, curr);
        free(curr);
    }
}

bool containsNearbyDuplicate(int* nums, int numsSize, int k)
{
    
    
    struct HashEntry* map = NULL;
    for(int i = 0;i < numsSize;i++)
    {
    
    
        if(hashFindItem(&map, nums[i]) != NULL)
        {
    
    
            if(i - hashFindItem(&map, nums[i])->val <= k)
            {
    
    
                hashFreeAll(&map);
                return true;
            }
        }
        hashAddItem(&map, nums[i], i);
    }
    hashFreeAll(&map);
    return false;
}
  1. Python language version
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        map = {
    
    }
        for i, num in enumerate(nums):
            if num in map:
                if i - map[num] <= k:
                    return True
            map[num] = i
        return False
  1. C++ language version
class Solution {
    
    
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
    
    
        unordered_map<int, int> map;
        for(int i = 0;i < nums.size();i++){
    
    
            if(map.count(nums[i]) != 0){
    
    
                if(i - map[nums[i]] <= k){
    
    
                    return true;
                }
            }
            map[nums[i]] = i;
        }
        return false;
    }
};

Ten【Submission Results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/132164468