struct hashTable {
int key;
int val;
UT_hash_handle hh;
};
int fourSumCount(int* A, int ASize, int* B, int BSize, int* C, int CSize, int* D, int DSize) {
struct hashTable* hashtable = NULL;
for (int i = 0; i < ASize; ++i)
{
for (int j = 0; j < BSize; ++j)
{
int ikey = A[i] + B[j];
struct hashTable* hash;
HASH_FIND_INT(hashtable, &ikey, hash);
if (hash == NULL) {
struct hashTable* hash = malloc(sizeof(struct hashTable));
hash->key = ikey;
hash->val = 1;
HASH_ADD_INT(hashtable, key, hash);
}
else
{
hash->val++;
}
}
}
int count = 0;
for (int i = 0; i < CSize; ++i)
{
for (int j = 0; j < DSize; ++j)
{
int ikey = -C[i] - D[j];
struct hashTable* hash;
HASH_FIND_INT(hashtable, &ikey, hash);
if (hash != NULL)
{
count += hash->val;
}
}
}
return count;
}
454. 四数相加 II
最新推荐文章于 2024-09-13 13:21:04 发布