12ms C solution


  • 0
    L

    once we got a number, we change a bit in memory to 1.
    by the adress of a bit 1 ,we can figure the number we have.
    for example ,if the numsize = 2, and we have {1,3}, in memory we got a
    0x00000006-> binary 1(3)01(1)0,last bit stand for zero.

    bool containDulicateNew(int* nums, int numsSize){
    	if ((!nums) || (numsSize < 2)){
    		return false;
    	}
    
    	char* numCount = (char*)malloc(0xFFFFF);	
    	memset(numCount, 0, 0xFFFFF);
        //offset = num address
    	int offset = 0;
    	int bit = 0;
    
    	for (int i = 0; i < numsSize; i++){  	    		
    		offset = nums[i] >> 3;   
                //negative number		
    		if(nums[i] & 0x80000000){
    			offset += 0xFFFF;
    		}    
    		bit = 0x01 << ( nums[i] & 0x07 );
    		
    		if (*(numCount + offset) & bit)
    		{
    			free(numCount);
    			return true;
    		}
    		
    		*(numCount + offset) |= bit;
    	}
    	free(numCount);
    	return false;
    }

  • 0
    D

    can you explain your code in detail . i have difficulty in reading it. thanks


  • 0
    L

    answer edited, hope can help.


Log in to reply
 

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