Share my C solution -> 2ms


  • 0
    W
    int removeElement(int* nums, int numSize, int val) {
        int count = 0;
        int *tmp = nums + numSize - 1;
    
        while(nums < tmp){
        	if(*nums != val)
        		nums++;
        	else{
        		if(*tmp == val){
        			tmp--;
        			count++;
        		}
        		else{
        			*nums++ = *tmp--;
        			count++;
        		}
        	}
        }
    
        if(nums == tmp && *tmp == val)
        	count++;
    
        return numSize - count;
    }

Log in to reply
 

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