LeetCode 1. 两数之和
原题链接
简单
作者:
浪味仙本仙
,
2020-06-14 22:06:34
,
所有人可见
,
阅读 2
struct Entry {
int id; /* we'll use this field as the key */
int val;
UT_hash_handle hh; /* makes this structure hashable */
};
struct Entry *g_users = NULL;
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *res = (int *)calloc(2, sizeof(int));
*returnSize = 2;
g_users = NULL;
for (int i = 0; i < numsSize; i++) {
struct Entry *s = (struct Entry *)calloc(1, sizeof(struct Entry));
struct Entry *tmp = NULL;
int r = target - nums[i];
HASH_FIND_INT(g_users, &r, tmp);
if (tmp != NULL) {
res[0] = tmp->val, res[1] = i;
return res;
}
s->id = nums[i];
s->val = i;
HASH_ADD_INT(g_users, id, s);
}
return res;
}