哈希表-四数相加II

题目

https://leetcode-cn.com/problems/4sum-ii/
在这里插入图片描述

思路

第一反应必是暴力,四个for循环解决。但据说运行的时候甚至会卡住,毕竟是O(n的四次方)。
那么就想到用数学中相反数的方式。
用HashMap先存入两个数组相加后的值存入到key中,其中可能会有重复的key,value就用于记录各个key对应的次数。显然两组是时间复杂度最低的。O(n的平方)
再用后两组计算其中的值,找到有-(和)=之前map存入的值,而输出的个数由各个value相加来决定
eg: 前两组key=2,value=3 找到后两组的和为-2,-2的相反数就是2,这时候result+=2。

代码实现以及书写会出现的问题

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer,Integer> map = new HashMap<>();
        int result = 0;
        //求出 A 和 B 任意两数之和 sumAB,以 sumAB 为 key,sumAB 出现的次数为 value,存入 hashmap 中
        for (int i:nums1) {
            for (int j:nums2) {
                if(map.containsKey(i+j)) {
                    map.put(i+j,map.get(i+j)+1);
                }
                else {map.put(i+j,1);}
            }
        }
        //然后计算 C 和 D 中任意两数之和的相反数 sumCD,在 hashmap 中查找是否存在 key 为 sumCD。
        for (int i:nums3) {
            for (int j:nums4) {
                int sum = -i-j;
                if (map.containsKey(sum)){
                    result+=map.get(-i-j);
                }
            }
        }
        return result;
    }
  1. result+=map.get(-i-j); 容易写成i+j(一定要明白,相反数才在map中)
  2. if(map.containsKey(i+j)) {
    map.put(i+j,map.get(i+j)+1);
    }
    只有在有的情况下才在对应value上+1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用哈希表来解决两数之和的问题。哈希表是一种将和值进行映射的数据结构,它可以快速地查找给定对应的值。以下是一个使用哈希表求解两数之和的示例代码: ```c #include <stdio.h> #include <stdbool.h> typedef struct { int key; int value; } HashNode; typedef struct { HashNode** table; int size; } HashMap; HashMap* createHashMap(int size) { HashMap* hashMap = (HashMap*)malloc(sizeof(HashMap)); hashMap->size = size; hashMap->table = (HashNode**)malloc(sizeof(HashNode*) * size); for (int i = 0; i < size; i++) { hashMap->table[i] = NULL; } return hashMap; } void put(HashMap* hashMap, int key, int value) { int hash = abs(key) % hashMap->size; while (hashMap->table[hash] != NULL && hashMap->table[hash]->key != key) { hash = (hash + 1) % hashMap->size; } if (hashMap->table[hash] != NULL) { free(hashMap->table[hash]); } HashNode* node = (HashNode*)malloc(sizeof(HashNode)); node->key = key; node->value = value; hashMap->table[hash] = node; } int get(HashMap* hashMap, int key) { int hash = abs(key) % hashMap->size; while (hashMap->table[hash] != NULL && hashMap->table[hash]->key != key) { hash = (hash + 1) % hashMap->size; } if (hashMap->table[hash] == NULL) { return -1; } else { return hashMap->table[hash]->value; } } bool containsKey(HashMap* hashMap, int key) { int hash = abs(key) % hashMap->size; while (hashMap->table[hash] != NULL && hashMap->table[hash]->key != key) { hash = (hash + 1) % hashMap->size; } return hashMap->table[hash] != NULL; } void freeHashMap(HashMap* hashMap) { for (int i = 0; i < hashMap->size; i++) { if (hashMap->table[i] != NULL) { free(hashMap->table[i]); } } free(hashMap->table); free(hashMap); } int* twoSum(int* nums, int numsSize, int target, int* returnSize) { HashMap* hashMap = createHashMap(numsSize); for (int i = 0; i < numsSize; i++) { int complement = target - nums[i]; if (containsKey(hashMap, complement)) { int* result = (int*)malloc(sizeof(int) * 2); result[0] = get(hashMap, complement); result[1] = i; *returnSize = 2; freeHashMap(hashMap); return result; } put(hashMap, nums[i], i); } freeHashMap(hashMap); return NULL; } int main() { int nums[] = {2, 7, 11, 15}; int target = 9; int returnSize; int* result = twoSum(nums, sizeof(nums) / sizeof(nums[0]), target, &returnSize); if (result != NULL) { printf("Indices: %d, %d\n", result[0], result[1]); free(result); } else { printf("No solution found.\n"); } return 0; } ``` 这段代码的思路是使用哈希表来存储每个元素的值和索引,然后遍历数组中的每个元素,查找是否存在与当前元素值相加等于目标值的另一个元素。如果存在,则返回这两个元素的索引。否则,返回NULL表示没有找到解决方案。 希望这可以帮助到你!如有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值