My c++ submission 16ms recursion


  • -1
    J

    class Solution {
    public:
    vector<int> searchRange(vector<int>& nums, int target) {
    vector<int> vector_range(2,-1);
    if (0 == nums.size()) return vector_range;
    int i = 0;
    int j = nums.size() - 1;

        search(nums,i,j,vector_range,target);
    }
    
    void search(vector<int>& nums,int begin,int end,vector<int>& vRange,int target)
    {
        if (begin == end)   
        {
            if(target == nums[begin])
            {
                if(begin > vRange[1])
                    vRange[1] = begin;
                if (begin<vRange[0] && vRange[0] != -1)
                    vRange[0] = begin;
                if (-1 == vRange[0])
                    vRange[0] = begin;
            }
            
            return;
        }
                    
            
        int mid = (begin+end)/2;
        
        if (target == nums[mid])
        {
            if(mid > vRange[1])
                vRange[1] = mid;
            if (mid<vRange[0] && vRange[0] != -1)
                vRange[0] = mid;
            if (-1 == vRange[0])
                vRange[0] = mid;
                
            search(nums,begin,mid,vRange,target);
            search(nums,mid+1,end,vRange,target);
            
        }
            
        else if(target > nums[mid])
            search(nums,mid+1,end,vRange,target);
        else
            search(nums,begin,mid,vRange,target);
    
        return;        
    }
    

    };


Log in to reply
 

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