Solution C++ 6ms


  • 0
    P
    int removeElement(vector<int>& nums, int val) {
    
    int length = nums.size();
    int idx = 0;
    
    if(length < 1)
        return length;
    
    for(;;)
    {
    	if(nums[idx] == val)
    	{
    		nums[idx] = nums[length-1];
    		length--;
    	}
    	else
    	{
    		idx++;
    	}
    
    	if(idx > length-1)
    		break;
    }
    
    return length;
    }

Log in to reply
 

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