LeetCode 33. Search for a Range

二分搜索,找到index后,考察index两边的元素是否仍等于target, 进而确定begin和end即可。


代码:

class Solution 
{
public:
    vector<int> searchRange(int A[], int n, int target) 
    {
    	vector<int> ret(2, -1);

    	int l=0, r=n-1, mid;
    	while (l <= r)
    	{
    		mid = (l+r) >> 1;
    		if (A[mid] == target)
    		{
    			break;
    		} else if (A[mid] < target)
    		{	
    			l = mid + 1;
    		} else
    		{
    			r = mid - 1;
    		}
    	}
    	if (A[mid] == target)
    	{
	    	for (ret[0]=mid; ret[0]-1>=0 && A[ret[0]-1]==target; -- ret[0]) {}
	    	for (ret[1]=mid; ret[1]+1< n && A[ret[1]+1]==target; ++ ret[1]) {}
		}

		return ret;
    }
};


你可能感兴趣的:(LeetCode,C++,search,binary)