LeetCode //C - 560. Subarray Sum Equals K

本文介绍了如何使用哈希表解决LeetCode问题560,即计算整数数组中和为k的子数组数量。通过线性探测哈希函数和自定义的哈希表结构,实现高效的求解算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

560. Subarray Sum Equals K

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

A subarray is a contiguous non-empty sequence of elements within an array.
 

Example 1:

Input: nums = [1,1,1], k = 2
Output: 2
Explanation:

Example 2:

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

Constraints:
  • 1 < = n u m s . l e n g t h < = 2 ∗ 1 0 4 1 <= nums.length <= 2 * 10^4 1<=nums.length<=2104
  • -1000 <= nums[i] <= 1000
  • − 1 0 7 < = k < = 1 0 7 -10^7 <= k <= 10^7 107<=k<=107

From: LeetCode
Link: 560. Subarray Sum Equals K


Solution:

Ideas:

This code defines a simple hash table implementation for demonstration purposes, using linear probing for collision resolution. It’s crucial to note that, in practice, there are more efficient and sophisticated hash table implementations available in libraries or you could use data structures available in the standard libraries of high-level languages that abstract these details away. The hashInsert function updates the count of each cumulative sum encountered, and the hashFind function retrieves the count of a particular sum. This allows us to efficiently count the number of subarrays that sum to k.

Code:
// Definition for the hash table's element
typedef struct {
    int key;    // The cumulative sum
    int value;  // How many times this sum has occurred
} HashElement;

// Simple hash function
unsigned int hashFunction(int key, int size) {
    unsigned int hash = (unsigned int)key;
    return hash % size;
}

// Insert or update the hash table
void hashInsert(HashElement *table, int size, int key, int increment) {
    unsigned int hash = hashFunction(key, size);
    while (table[hash].value != 0 && table[hash].key != key) {
        hash = (hash + 1) % size; // Linear probing
    }
    if (table[hash].value == 0) { // New insertion
        table[hash].key = key;
    }
    table[hash].value += increment;
}

// Find a value by key in the hash table
int hashFind(HashElement *table, int size, int key) {
    unsigned int hash = hashFunction(key, size);
    while (table[hash].value != 0) {
        if (table[hash].key == key) {
            return table[hash].value;
        }
        hash = (hash + 1) % size; // Linear probing
    }
    return 0; // Not found
}

// Function to calculate the number of subarrays that sum up to k
int subarraySum(int* nums, int numsSize, int k) {
    int count = 0, sum = 0;
    int tableSize = numsSize * 2; // To reduce collision probability
    HashElement *table = (HashElement *)calloc(tableSize, sizeof(HashElement));

    // Handle the case when the running sum equals k itself
    hashInsert(table, tableSize, 0, 1);

    for (int i = 0; i < numsSize; i++) {
        sum += nums[i];
        count += hashFind(table, tableSize, sum - k);
        hashInsert(table, tableSize, sum, 1);
    }

    free(table); // Don't forget to free the allocated memory!
    return count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值