454哈希表-四数相加

题目

 链接:454. 四数相加 II - 力扣(LeetCode)

思路

  1. 定义一个map,key存放两数之和,value存放两数之和出现的次数
  2. 遍历前两个数组,统计两数之和,两数之和出现的次数,放到map中
  3. 定义res 存放四数之和为0出现的次数
  4. 遍历后两个数组,如果找到0-(前两数组之和)在map中出现过的话,就用res加上value对应的值来统计对应次数

代码

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4){
        Map<Integer,Integer> map = new HashMap<>();
        int temp;
        int res = 0;
        //统计两个数组中的元素之和,同时统计出现的次数,放入map中
        for(int i : nums1){
            for(int j : nums2){
                temp = i + j;
                if(map.containsKey(temp)){
                    map.put(temp,map.get(temp) + 1);
                } else {
                    map.put(temp,1);
                }
            }
        }
        //统计剩余的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数
        for(int i : nums3){
            for(int j : nums4){
                temp = i + j;
                if(map.containsKey(0 - temp)){
                    res += map.get(0 - temp);
                }
            }
        }
        return res;
    }
}

  • 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、付费专栏及课程。

余额充值