ac java recursion solution


  • 0
    R
    public class Solution {
        public int search(int[] nums, int target) {
            if(nums==null || nums.length==0)    return -1;
            if(nums.length==1)  return nums[0]==target?0:-1;
            return binarySearch(nums, 0, nums.length-1, target);
        }
        
        public int binarySearch(int[] nums, int lo, int hi, int target){
            if(lo+1>=hi)    return nums[lo]==target?lo:(nums[hi]==target)?hi:-1;
            int mid = lo+ (hi-lo)/2;
            if(nums[mid]==target)   return mid;
            int left=binarySearch(nums, lo, mid-1, target);
            int right=binarySearch(nums, mid+1,hi,target);
            if(left>=0)  return  left;
            if(right>=0) return right;
            return -1;
        }
    }
    

  • 0
    2

    what does "ac" mean?


  • 0
    R

    @20170601 accepted


Log in to reply
 

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