C solution . create hash table by c


  • -5
    W
    enter code heretypedef struct node_s node;
    

    struct node_s {
    int val;
    node next;
    };
    typedef struct {
    node
    head;
    nodelast;
    }hash;
    int myabs(int a){
    return a>=0?a:-a;
    }
    bool containsDuplicate(int
    nums, int numsSize) {
    hash table=(hash)malloc(sizeof(hash)100);
    int i,j,index;
    for(i=0;i<100;i++) table[i].head=table[i].last=NULL;
    for(i=0;i<numsSize;i++){
    index=myabs(nums[i])%100;
    if(table[index].head==NULL){
    table[index].head=(node
    )malloc(sizeof(node));
    table[index].head->val=nums[i];
    table[index].last=table[index].head;
    table[index].last->next=NULL;
    }else{
    nodep=table[index].head;
    while(p){
    if(p->val==nums[i]) return true;
    p=p->next;
    }
    p=(node
    )malloc(sizeof(node));
    p->val=nums[i];
    p->next=NULL;
    table[index].last->next=p;
    table[index].last=p;
    }
    }
    return false;
    }


  • 0
    F

    Could you please add indentation to your code? Thanks in advance.


Log in to reply
 

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