java binary search


  • 0
    2
    public class Solution {
        public int mySqrt(int x) {
            long lo = 0;
            long hi = x;
            while (lo < hi) {
                long mid = (lo + hi + 1) / 2;
                if (mid * mid > x) hi = mid - 1;
                else lo = mid;
            }
            return (int)lo;
        }
    }
    

Log in to reply
 

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