Eclipse results Different than OJ for problem "Remove Duplicates from Sorted Array II"


  • 0
    L

    Hi,

    When I submitted my code in OJ, I always got "wrong answer":

    Input:
    [-3,-1,-1,0,0,0,0,0]
    Output:
    [-3,-1,-1]
    Expected:
    [-3,-1,-1,0,0]

    BUT when running locally, I got the right answer from console
    "
    new length is 5.
    -3 -1 -1 0 0
    "

    my main function:

    int main(){
    	int array[8] = {-3,-1,-1,0,0,0,0,0};
    	int newL = removeDuplicates(array, 8);
    	printf("new length is %d\n", newL);
    	for(int j = 0; j < newL; j++){
    		printf("%d ", array[j]);
    	}
    	return 0;
    }
    

    Any reason why? I checked step by step but didn't find anything wrong and didn't use static
    variable either. Any help would be appreciated.

    thanks,

    My code:

    int removeDuplicates(int* nums, int numsSize) {
        
    	if( numsSize <= 2){                                               // array has less than 3 elements
    		return numsSize;
    	}
    	else if( numsSize == 3 ){                                         // array has 3 elements
    		if( nums[0] == nums[1] == nums[2]){
    			return 2;
    		}
    		else{
    			return 3;
    		}
    	}
    	else{                                                             // array has more than 3 elements
    
    		int *p1 = nums, *p2 = nums+1, *p3;
    		int arraySize = numsSize, removeNum;
    
    		for(int i = 0; i < arraySize-2; i++){
    			p3 = p2+1;
    			if( *p1 == *p2 && *p2 == *p3){
    				removeNum = 0;
    				while( *p2 == *p3 ){
    					removeNum++;
    					p3++;
    				}
    				for(int j = 0; j < arraySize-i-2; j++){
    					int *p_index = p2+1;
    					p_index[j] = p_index[j+removeNum];
    				}
    				p1+=2;
    				p2+=2;
    				arraySize-=removeNum;
    			}
    			else{
    				p1++;
    				p2++;
    			}
    		}
    		return arraySize;
    	}
        
    }

  • 0
    D

    int removeDuplicates(int* nums, int numsSize) {
    if( numsSize <= 2){ // array has less than 3 elements
    return numsSize;
    }
    else if( numsSize == 3 ){ // array has 3 elements
    if( nums[0] == nums[1]&&nums[1] == nums[2]){//use && to connect
    return 2;
    }
    else{
    return 3;
    }
    }
    else{ // array has more than 3 elements

        int *p1 = nums, *p2 = nums+1, *p3=NULL;//p2 always equals p1+1
        int *pEnd=nums+numsSize;//pay attention to this
        int arraySize = numsSize;
        int removeNum;
    
        for(int i = 0; i < arraySize-2; i++){
            p3 = p2+1;
            if( *p1 == *p2 && *p2 == *p3){
                removeNum = 0;
                while( *p2 == *p3 &&p3<pEnd){//pay attention to this
                    removeNum++;
                    p3++;
                }
                for(int j = 0; j < arraySize-i-2; j++){
                    int *p_index = p2+1;
                    p_index[j] = p_index[j+removeNum];
                }
                p1+=2;
                p2+=2;
                arraySize-=removeNum;
                pEnd-=removeNum;//pay attention to this
            }
            else{
                p1++;
                p2++;
            }
        }
        return arraySize;
    }
    

    }//ac code


Log in to reply
 

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