哈希表-四数相加II

42 篇文章 2 订阅
22 篇文章 0 订阅
本文介绍了一个编程题目,要求计算四个整数数组中满足四数之和为零的元组数量。作者给出了使用自定义哈希表(HashMap)进行优化的解决方案,通过映射和查找操作减少计算复杂度。
摘要由CSDN通过智能技术生成
力扣题号:454. 四数相加 II

一、题目描述

给你四个整数数组 nums1nums2nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

二、示例

示例 1:

输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0

示例 2:

输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
输出:1

三、求解思路

四、代码实现

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 99999999

// 自定义数组
typedef struct array {
	int* arr = NULL;
	int size = 0;
}Array;

// map元素
typedef struct map {
	int key;
	int value;
}MapNode;

// 自定义哈希map
typedef struct hash_map {
	MapNode arr[1005];
}HashMap;
// 映射函数
void voidreflection(HashMap& H, int n) {

	int n1 = n % 1000;
	while (H.arr[n1].key != MAX && H.arr[n1].key != n) {
		n1++;
		n1 %= 1000;
	}
	if (H.arr[n1].key == n) { // 元素重复
		H.arr[n1].value++;
		return;
	}
	H.arr[n1].key = n;
	H.arr[n1].value++;
}
// 查找哈希表中是否有该元素
int find(HashMap& H, int n) {
	int n1 = n % 1000;
	while (H.arr[n1].key != n && H.arr[n1].key != MAX) {
		n1++;
		n1 %= 1000;
	}
	if (H.arr[n1].key == n) {
		return H.arr[n1].value;
	}
	return 0;
}

// 四数相加
int solution(Array& A, Array& B, Array& C, Array& D) {
	HashMap map; // 自定义哈希表
	for (int i = 0; i < 1005; i++) {
		map.arr[i].key = MAX;
		map.arr[i].value = 0;
	}
	
	for (int i = 0; i < A.size; i++) {
		for (int j = 0; j < B.size; j++) {
			voidreflection(map, A.arr[i] + B.arr[j]);
		}
	}
	int count = 0; // 统计a+b+c+d = 0 出现的次数
	for (int i = 0; i < C.size;i++) {
		for (int j = 0; j < D.size; j++) {
			count += find(map, 0 - (C.arr[i] + D.arr[j]));
		}
	}
	return count;
}

// 打印数组
void Print(Array& A) {
	for (int i = 0; i < A.size; i++) {
		printf("-----");
	}
	printf("\n");
	/*for (int i = 0; i < A.size; i++) {
		printf("%5d", i);
	}*/
	//printf("\n");
	for (int i = 0; i < A.size; i++) {
		printf("%5d", A.arr[i]);
	}
	printf("\n");
	for (int i = 0; i < A.size; i++) {
		printf("-----");
	}
	printf("\n");
}

int main() {
	// nums1 =[1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
	Array arr1, arr2, arr3, arr4;
	int a1[] = { 1,2 };
	int a2[] = { -2,-1 };
	int a3[] = { -1,2 };
	int a4[] = { 0,2 };
	int a5[] = { 0 };
	int a6[] = { 0 };
	int a7[] = { 0 };
	int a8[] = { 0 };
	arr1.size = arr2.size = arr3.size = arr4.size = 2;
	arr1.arr = a1;
	arr2.arr = a2;
	arr3.arr = a3;
	arr4.arr = a4;
	
	Print(arr1); Print(arr2); Print(arr3); Print(arr4);
	// 四数相加
	int count = 0;
	count = solution(arr1,arr2,arr3,arr4);
	printf("\n count = %2d\n\n", count);

	// nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
	arr1.size = arr2.size = arr3.size = arr4.size = 1;
	arr1.arr = a5;
	arr2.arr = a6;
	arr3.arr = a7;
	arr4.arr = a8;
	Print(arr1); Print(arr2); Print(arr3); Print(arr4);
	count = solution(arr1, arr2, arr3, arr4);
	printf("\n count = %2d\n\n", count);

	return 0;
}

五、运行结果

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值