C++ using lower_bound


  • 0
    T
    int singleNonDuplicate(vector<int>& nums) 
    {
    	if(nums.size()==1)
    		return nums.back();
    	if(nums.back()!=*(nums.end()-2))
    		return nums.back();
    	return *lower_bound(nums.begin(),nums.end()-1,0,[&nums](int& a,int b)->bool
    	{
    		if((&a-&*nums.begin())%2==0)
    		{
    			if(a==*(&a+1))
    				return true;
    			else
    				return false;
    
    		}
    		else
    		{
    			if(a==*(&a+1))
    				return false;
    			else
    				return true;
    		}
    	});
    }
    

    We can define the operator and use lower_bound function


Log in to reply
 

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