Java binary search without using long


  • 1
    S
        public boolean isPerfectSquare(int num) {
            if (num == 0) return true;
            int start = 1;
            int end = num;
            while (start < end - 1) {
                int mid = start + (end - start) / 2;
                if (mid == num / mid && num % mid == 0) return true;
                if (mid > num / mid) {
                    end = mid;
                } else {
                    start = mid;
                }
            }
            if ((num / start == start && num % start == 0) || (num % end == 0 && num / end == end))  return true;
            else return false;
        }
    

Log in to reply
 

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