//哈希表
typedef struct//自定义哈希表的结构
{
int key;
int val;
UT_hash_handle hh;//初始化为NULL
} hashTable;
int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
hashTable* hashtable=NULL;
for(int i=0;i<numsSize;i++)
{
int key=target-nums[i];
hashTable* temp=NULL;
HASH_FIND_INT(hashtable,&key,temp);//哈希表查找有没有满足条件的值
if(temp!=NULL)//有则返回
{
int* ret = malloc(sizeof(int)*2);//为这两个下标申请空间
ret[0]=temp->val;
ret[1]=i;
*returnSize=2;
return ret;
}
else//没有则插入hash表
{
temp=(hashTable*)malloc(sizeof(hashTable));
temp->key=nums[i];
temp->val=i;
HASH_ADD_INT(hashtable,key,temp);
}
}
*returnSize = 0;
return NULL;//没有
}