Java Binary search solution


  • 0
    R
        public static int sqrt (int a){
            int low =0;
            int high = a;
    
            while (low <= high){
                int mid = (low+high)/2;
    
                if (mid*mid == a)
                    return mid;
    
                if (mid*mid > a){
                    high = mid-1;
                }
                else {
                    low = mid + 1;
                }
            }
            return high;
        }
    

Log in to reply
 

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