clueless coding // TODO: be smarter

LeetCode Valid Perfect Square: CPP Solution



TL;DR Code Solution


bool isPerfectSquare(int num) {

    long low = 1;
    long high = num;

    while (low <= high) {

        long mid = (low + high) / 2;
        long square = mid * mid;
        if (square < num) {
            low = mid + 1;
        }
        else if (square > num) {
            high = mid - 1;
        }
        else {
            return true;
        }
    }

    return false;

}