My solution to the question


  • 0
    V
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            vector<int> result( 2, -1 );
    
            int left = 0;
            int mid;
            int right = nums.size() - 1;
            while( left <= right )
            {
                mid = ( left + right ) / 2;
                if( nums[ mid ] > target )
                    right = mid - 1;
    
                else if( nums[ mid ] < target )
                    left = mid + 1;
    
                else
                {
                    for( left = mid; left >= 0; left-- )
                        if( nums[ left ] != nums[ mid ] )
                            break;
                    result[ 0 ] = left + 1 ;
                
                    for( right = mid; right < nums.size(); right++ )
                        if( nums[ right ] != nums[ mid ] )
                            break;
                    result[ 1 ] = right - 1;
                
                    break;
                }
            }
            return result;
        }
    };

Log in to reply
 

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