Contains Duplicate


  • 0
    D
    #define HASHSIZE 101
    static struct nlist *hashtab[HASHSIZE] = {NULL};
    struct nlist{
            struct nlist *next;
            int num;
    };
    
    bool containsDuplicate(int* nums, int numsSize) {
        int i;
        for(i = 0; i < numsSize; i++ )
        {
            struct nlist *np;
            unsigned hashval;
            for(np = hashtab[abs(nums[i])%HASHSIZE]; np != NULL; np = np->next)
            {
                if(nums[i] == np->num)
                {
                    return true;
                }
            }
            np = (struct nlist *)malloc(sizeof(*np));
            np->num = nums[i];
            hashval = abs(nums[i])%HASHSIZE;
            np->next = hashtab[hashval];
            hashtab[hashval] = np;
            
        }
        return false;
        
    }
    

    the run code result and submit solution give different answers
    [3,1]
    Your answer

    false
    Expected answer

    false

    Input:
    [3,1]
    Output:
    true
    Expected:
    false


Log in to reply
 

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