Valid Perfect Square
description
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
Returns: TrueExample 2:
Input: 14
Returns: False
thinking
使用分数指数次幂的方法,求num的0.5次幂,实际上就是做了sqrt
solution
class Solution {
public:
bool isPerfectSquare(int num)
{
return (int)(pow(num, 0.5)) == pow(num, 0.5) ? true : false;
}
};