C语言 | Leetcode C语言题解之第454题四数相加II

题目:

题解:

cpp 复制代码
struct hashTable {
    int key;
    int val;
    UT_hash_handle hh;
};

int fourSumCount(int* A, int ASize, int* B, int BSize, int* C, int CSize, int* D, int DSize) {
    struct hashTable* hashtable = NULL;
    for (int i = 0; i < ASize; ++i) {
        for (int j = 0; j < BSize; ++j) {
            int ikey = A[i] + B[j];
            struct hashTable* tmp;
            HASH_FIND_INT(hashtable, &ikey, tmp);
            if (tmp == NULL) {
                struct hashTable* tmp = malloc(sizeof(struct hashTable));
                tmp->key = ikey, tmp->val = 1;
                HASH_ADD_INT(hashtable, key, tmp);
            } else {
                tmp->val++;
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < CSize; ++i) {
        for (int j = 0; j < DSize; ++j) {
            int ikey = -C[i] - D[j];
            struct hashTable* tmp;
            HASH_FIND_INT(hashtable, &ikey, tmp);
            if (tmp != NULL) {
                ans += tmp->val;
            }
        }
    }
    return ans;
}
相关推荐
byte轻骑兵5 分钟前
【安全函数】C语言安全字符串函数详解:告别缓冲区溢出的噩梦
c语言·安全·面试
玖剹1 小时前
穷举 VS 暴搜 VS 深搜 VS 回溯 VS 剪枝
c语言·c++·算法·深度优先·剪枝·深度优先遍历
Ch_ty2 小时前
leetcode解题思路分析(一百六十八)1452 - 1458 题
算法·leetcode·哈希算法
红豆诗人2 小时前
C语言进阶知识--文件操作
c语言·开发语言·文件操作
pilgrim5311 小时前
结合 Leetcode 题探究KMP算法
算法·leetcode
2401_8612775514 小时前
软考程序员2016年上半年二叉排序树案例题解答
c语言·决策树·链表
前进的李工14 小时前
LeetCode hot100:234 回文链表:快慢指针巧判回文链表
python·算法·leetcode·链表·快慢指针·回文链表
sin_hielo14 小时前
leetcode 3228
算法·leetcode
xier_ran15 小时前
力扣(LeetCode)100题:41.缺失的第一个正数
数据结构·算法·leetcode
Swift社区15 小时前
LeetCode 425 - 单词方块
算法·leetcode·职场和发展