求平方根是否是整数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35747066/article/details/89431676

class Solution {
public:
    bool isPerfectSquare(int num) {
        if(num == 1){return true;}
        int low = 1;
        int high = num / 2;
        while(low <= high){
            long mid = (low + high) / 2;
            if(mid * mid == num){return true;}
            else if(mid * mid < num){
                low = mid + 1;
            }
            else if(mid * mid > num){
                high = mid - 1;
            }
        }
        return false;
    }
};

367. Valid Perfect Square

Easy

42597FavoriteShare

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Output: true

Example 2:

Input: 14
Output: false

猜你喜欢

转载自blog.csdn.net/qq_35747066/article/details/89431676