BST solution


  • 0
    S

    int findFirstOccurence(vector<int> &A,int target,bool s){

        int start = 0; 
        int end = A.size() - 1; 
        int mid;
        int ret = -1;
        while(start<= end){
            mid = (end-start)/2 + start;
            if(A[mid] == target){
                if(s){
                  ret = mid; 
                  end = mid - 1; 
                }
                else{
                  ret = mid;
                  start = mid + 1;
                }
            }
            else if ( A[mid] > target){ 
               end = mid - 1; 
            }
            else{ 
               start = mid + 1;
            }
        }
        return ret;
    }
    
    vector<int> searchRange(vector<int>& nums, int target) {
        
        vector<int> ret;
        ret.emplace_back(findFirstOccurence(nums,target,true));
        ret.emplace_back(findFirstOccurence(nums,target,false));
        return ret;
    }

Log in to reply
 

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