Binary Search Java


  • 0
    B
    public class Solution {
        public boolean isPerfectSquare(int num) {
            if (num < 1) {
                return false;
            }
            
            long start = 1;
            long end = num;
            while (start + 1 < end) {
                long mid = (end - start) / 2 + start;
                if (mid * mid == num) {
                    return true;
                } else if (mid * mid < num) {
                    start = mid;
                } else {
                    end = mid;
                }
            }
            
            if (start * start == num || end * end == num) {
                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.