JAVA AC Recursive solution


  • 0
    H

    '''
    public class Solution {
    public int search(int[] nums, int target) {
    int len = nums.length;
    return BinarySearch(nums, 0, len - 1, target);
    }

    public int BinarySearch(int[] nums, int lo, int hi, int val){
        if(lo < 0 || hi < 0 || lo > hi) return -1;
        int mid = (lo + hi) / 2;
        if(nums[mid] == val) return mid;
        int loc = -1;
        loc = BinarySearch(nums, lo, mid - 1, val);
        if(loc == -1)
            loc = BinarySearch(nums, mid + 1, hi, val);
        
        return loc;
    }
    

    }'''


Log in to reply
 

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