题目:
解题思路:
题目中要求元素成数对出现,即每个元素出现偶数次。用哈希表存放每个数出现的次数,再循环查看每个数的次数是否位偶数。
typedef struct {
int key;
int count;
UT_hash_handle hh;
} hashEntry;
bool divideArray(int* nums, int numsSize) {
hashEntry * cnt = NULL;
for (int i = 0; i < numsSize; ++i) {
hashEntry * pEntry = NULL;
HASH_FIND(hh, cnt, &nums[i], sizeof(int), pEntry);
if (pEntry == NULL) {
pEntry = (hashEntry*)malloc(sizeof(hashEntry));
pEntry -> key = nums[i];
pEntry -> count = 1;
HASH_ADD(hh, cnt, key,sizeof(int), pEntry);
}
else pEntry -> count++;
}
hashEntry *curr = NULL, *next = NULL;
HASH_ITER(hh, cnt, curr, next)
{
if (curr->count % 2 != 0) return false;
}
return true;
}