C Language Construction Hash Table solution LC majority Element problem

Source: Internet
Author: User

Leetcode on the majority element is the solution of the Time O (N), Space O (1).

https://leetcode.com/problems/majority-element/

Use hash table to solve the time O (n), space O (n). It is convenient to use HashMap in Java. A classmate asked how to use C language to construct hash table. I wrote one of them:

typedefstructNode {intVal, Count;} Node;typedefstructHASH {Node*NP; intsize, capacity;} HASH;#defineN 509#defineHash_val (N) Abs ((n)% n)Static intEnsurecapacity (HASH *hp) {    intsize, capacity; Node*NP; Size= hp->size; Capacity= hp->capacity; if(Size <capacity)return 0; if(Capacity = =0) Capacity=8; Elsecapacity<<=1; NP= (node*)realloc(HP-&GT;NP, capacity *sizeof(Node)); if(NP = =NULL)return-1; HP->capacity =capacity; HP-&GT;NP =NP; return 0;}Static voidFreehashtab (HASH htab[],intN) {    inti;  for(i =0; I < n; i++)        if(htab[i].np) Free(HTAB[I].NP); }intMajorityelement (intArr[],intN) {HASH htab[n],*HB; intI, J, cur, hval, res; memset (Htab,0+ I *sizeof(HASH));  for(i =0; I < n; i++) {cur=Arr[i]; Hval=hash_val (cur); HB= &Htab[hval];  for(j =0; J < hb->size; J + +)            if(Hb->np[j].val = =cur) Break; if(j = = Hb->size) {            if(Ensurecapacity (HB) = =-1)                Gotoerr; HB->np[j].val =cur; HB->np[j].count =1; HB->size++; } Else{HB->np[j].count++; }        if(Hb->np[j].count > N/2) {res= hb->Np[j].val;            Freehashtab (Htab, N); returnRes;    }} err:freehashtab (Htab, N); return-1; }
View Code

The code is a little bit longer and there is definitely a bug. This time I ran into two questions:

1. When I expand the array, the code I started with is this:

    HP->NP = (node*)reallocsizeof(Node);     if (HP->NP = = NULL        ) return -1;

The problem here is that if realloc returns NULL, the original memory will not be erased. So here's a memory leak.

2. A hash function is required to map N to [0, N-1]. For a simple account, my hash function is simply defined as:

#define N 509#define hash_val (n) (n)% n

That's not right. Consider n = =-1, then the hash value is-1.

Again:

#define Hash_val (n) ABS (n)% n

It seems to be OK. But still does not, hehe, the test case on the LC is designed very well. Consider that n is-2147483648, then ABS (n) =-2147483648.

Note: 2147483648 exceeds the 32-bit int range, so abs (-2147483648) is undefined behavior.

Finally, replace the following:

#define Hash_val (N) Abs ((n)% n)

C Language Construction Hash Table solution LC majority Element problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.