dongsubak
3/1/2019 - 10:44 PM

binary search

var isPerfectSquare = function(num) {
    var min = 1;
    var max = num;
    while (min<=max) {
        var mid = parseInt(min+(max-min)/2)
        if (mid*mid == num) {
            return true;
        } else if (mid < num/mid) {
            min = mid+1
        } else {
            max = mid-1
        }
    }
    return false;
};