Hash table in C, I dont know why it is a wrong answer


  • 0

    Here is my code:

    bool containsDuplicate(int* nums, int numsSize) {
        int hashmod = numsSize,hashtable[numsSize-1],i,j,k,hashvalue;
        bool result=false;   
        for(i=0;i<numsSize;i++)
        {
            hashvalue = nums[i]%hashmod;
            if(hashtable[hashvalue]==NULL)
                hashtable[hashvalue] = nums[i];
            else
            {            
                if(hashtable[hashvalue]==nums[i])
                    result = true;
                else
                    {
                        for(j=1;j<numsSize;j++)
                        {
                            hashvalue = (nums[i]+j)%numsSize;
                            if(hashtable[hashvalue]==NULL)
                                 hashtable[hashvalue] = nums[i];
                            else
                                if(hashtable[hashvalue]==nums[i])
                                    result = true;
                         }
                    }
            }
        }
        return result;    
    }
    

    The test case is [1,2,3,4] and it turns out to output wrong answer, I dont understand. T T
    Besides, when I defined all elements in the array hashtable -1 at first instead of undefining them, the problem was gone but I dont see the difference.

    Thanks so much for your advice!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.