136. 只出现一次的数字(LeetCode)

struct hashTable
{
    int key;
    int value;
    UT_hash_handle hh;
};

int singleNumber(int* nums, int numsSize)
{
    struct hashTable *hashtable = NULL; 
    for (int i = 0; i < numsSize; i++) 
    {
        struct hashTable *tmp;
        HASH_FIND_INT(hashtable, nums + i, tmp);
        if (tmp) 
        {
            tmp->value++;
        }
        else 
        {
            struct hashTable *tmp = malloc(sizeof(struct hashTable));
            tmp->key = nums[i];
            tmp->value = 1;
            HASH_ADD_INT(hashtable, key, tmp); 
        }
    }
    struct hashTable *res;
    struct hashTable *tmp;
    HASH_ITER(hh, hashtable, res, tmp) 
    {                            
        if (res->value == 1)
        {
            return res->key;
        }
    }
    return 0;
}

注:用到了Hash函数,所以要加上头文件uthash.h

猜你喜欢

转载自blog.csdn.net/qq_59414507/article/details/120857568