一、题目
二、代码
class Solution
{
public :
vector< int > searchRange ( vector< int > & nums, int target)
{
int i, j;
int left, right, middle;
vector< int > return_vector ( 2 , - 1 ) ;
if ( nums. size ( ) < 3 )
{
for ( i= 0 ; i< nums. size ( ) ; i++ )
{
if ( nums[ i] == target)
{
return_vector[ 0 ] = i;
break ;
}
}
for ( i= nums. size ( ) - 1 ; i>= 0 ; i-- )
{
if ( nums[ i] == target)
{
return_vector[ 1 ] = i;
break ;
}
}
}
else
{
left= 0 ;
right= nums. size ( ) - 1 ;
middle= ( left+ right) / 2 ;
while ( left<= right- 2 )
{
if ( nums[ middle] == target) break ;
else if ( nums[ middle] < target)
{
left= middle;
middle= ( left+ right) / 2 ;
}
else if ( nums[ middle] > target)
{
right= middle;
middle= ( left+ right) / 2 ;
}
}
if ( nums[ middle] != target&& nums[ right] == target) middle= right;
if ( nums[ middle] == target)
{
return_vector= { middle, middle} ;
for ( i= middle; i>= 0 ; i-- )
{
if ( nums[ i] == target) return_vector[ 0 ] = i;
}
for ( i= middle; i< nums. size ( ) ; i++ )
{
if ( nums[ i] == target) return_vector[ 1 ] = i;
}
}
}
return return_vector;
}
} ;
三、运行结果