2023-01-29 00:08:12 +08:00
|
|
|
/**
|
2023-05-09 00:36:18 +08:00
|
|
|
* File: two_sum.c
|
2023-01-29 00:08:12 +08:00
|
|
|
* Created Time: 2023-01-19
|
|
|
|
* Author: Reanon (793584285@qq.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.h"
|
2023-01-29 00:08:12 +08:00
|
|
|
|
2023-02-03 18:53:15 +08:00
|
|
|
/* 方法一:暴力枚举 */
|
2023-01-29 00:08:12 +08:00
|
|
|
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
|
|
|
|
for (int i = 0; i < numsSize; ++i) {
|
|
|
|
for (int j = i + 1; j < numsSize; ++j) {
|
|
|
|
if (nums[i] + nums[j] == target) {
|
2023-04-18 14:31:23 +08:00
|
|
|
int *res = malloc(sizeof(int) * 2);
|
|
|
|
res[0] = i, res[1] = j;
|
2023-01-29 00:08:12 +08:00
|
|
|
*returnSize = 2;
|
2023-04-18 14:31:23 +08:00
|
|
|
return res;
|
2023-01-29 00:08:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*returnSize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 哈希表 */
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct {
|
2023-01-29 00:08:12 +08:00
|
|
|
int key;
|
|
|
|
int val;
|
2023-04-18 21:19:57 +08:00
|
|
|
UT_hash_handle hh; // 基于 uthash.h 实现
|
2023-10-18 02:16:26 +08:00
|
|
|
} HashTable;
|
2023-01-29 00:08:12 +08:00
|
|
|
|
2023-04-18 14:31:23 +08:00
|
|
|
/* 哈希表查询 */
|
2023-10-18 02:16:26 +08:00
|
|
|
HashTable *find(HashTable *h, int key) {
|
|
|
|
HashTable *tmp;
|
2023-01-29 00:08:12 +08:00
|
|
|
HASH_FIND_INT(h, &key, tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-04-18 14:31:23 +08:00
|
|
|
/* 哈希表元素插入 */
|
2024-11-10 02:00:53 +08:00
|
|
|
void insert(HashTable **h, int key, int val) {
|
|
|
|
HashTable *t = find(*h, key);
|
2023-01-29 00:08:12 +08:00
|
|
|
if (t == NULL) {
|
2023-10-18 02:16:26 +08:00
|
|
|
HashTable *tmp = malloc(sizeof(HashTable));
|
2023-01-29 00:08:12 +08:00
|
|
|
tmp->key = key, tmp->val = val;
|
2024-11-10 02:00:53 +08:00
|
|
|
HASH_ADD_INT(*h, key, tmp);
|
2023-01-29 00:08:12 +08:00
|
|
|
} else {
|
|
|
|
t->val = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:53:15 +08:00
|
|
|
/* 方法二:辅助哈希表 */
|
2023-01-29 00:08:12 +08:00
|
|
|
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
2023-10-18 02:16:26 +08:00
|
|
|
HashTable *hashtable = NULL;
|
2023-01-29 00:08:12 +08:00
|
|
|
for (int i = 0; i < numsSize; i++) {
|
2023-10-18 02:16:26 +08:00
|
|
|
HashTable *t = find(hashtable, target - nums[i]);
|
2023-01-29 00:08:12 +08:00
|
|
|
if (t != NULL) {
|
2023-04-18 14:31:23 +08:00
|
|
|
int *res = malloc(sizeof(int) * 2);
|
|
|
|
res[0] = t->val, res[1] = i;
|
2023-01-29 00:08:12 +08:00
|
|
|
*returnSize = 2;
|
2023-04-18 14:31:23 +08:00
|
|
|
return res;
|
2023-01-29 00:08:12 +08:00
|
|
|
}
|
2024-11-10 02:00:53 +08:00
|
|
|
insert(&hashtable, nums[i], i);
|
2023-01-29 00:08:12 +08:00
|
|
|
}
|
|
|
|
*returnSize = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-04-17 21:13:15 +08:00
|
|
|
/* Driver Code */
|
2023-01-29 00:08:12 +08:00
|
|
|
int main() {
|
|
|
|
// ======= Test Case =======
|
|
|
|
int nums[] = {2, 7, 11, 15};
|
2023-05-09 00:36:18 +08:00
|
|
|
int target = 13;
|
2023-01-29 00:08:12 +08:00
|
|
|
// ====== Driver Code ======
|
|
|
|
int returnSize;
|
|
|
|
int *res = twoSumBruteForce(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
|
|
|
// 方法一
|
|
|
|
printf("方法一 res = ");
|
|
|
|
printArray(res, returnSize);
|
|
|
|
|
|
|
|
// 方法二
|
|
|
|
res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
|
|
|
printf("方法二 res = ");
|
|
|
|
printArray(res, returnSize);
|
2023-04-17 22:15:06 +08:00
|
|
|
|
2023-01-29 00:08:12 +08:00
|
|
|
return 0;
|
2024-11-10 02:00:53 +08:00
|
|
|
}
|