777. Valid Perfect Square

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

注意事项

Do not use any built-in library function such as sqrt.
样例
For example:
Given num = 16
Returns True

二分查找

没什么说的,二分查找基本功。

 bool isPerfectSquare(int num) {
        int beg=0;
        int end=num;
        int mid;
        while(beg<=end)
        {
            mid=beg+(end-beg)/2;
            if(pow(mid,2)==num)
            return true;
            else if(pow(mid,2)

你可能感兴趣的:(777. Valid Perfect Square)