JAVA SOLUTION using binary search


  • 0
    N

    public class Solution {
    public boolean search(int[] nums, int target) {

        int length = nums.length;
        
        return check(nums, 0,length-1, target);
        
    }
    
    public boolean check(int[] nums, int start, int end, int target)
    {
        if(start > end)
        {
            return false;
        }
        int mid = (start + end)/2;
        
        if(nums[mid] == target)
        {
            return true;
        }
        
        return check(nums, start, mid-1,target) || check(nums, mid+1, end, target);
    }
    

    }


  • 4
    H

    This is average O(n), although the worst case is O(n) for any implementation. You have to abandon half if you want to get average O(logn)


Log in to reply
 

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