Use two "mid" makes life easier?


  • 0
    C

    In Some binary search, we can try to use two "mid" pointer. Sometimes, it will make life easier.

     public int findPeakElement(int[] nums) {
        //binary search O(logn) time
        int l=0,h=nums.length-1;
        while(l<h){
            int m1 =(h-l)/2+l;
            int m2 = m1+1;
            if(nums[m1]>nums[m2]) h=m1;
            else l=m2;
        }
        return l;
    }

  • 0
    L
    This post is deleted!

Log in to reply
 

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