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
想法:完全平方数是等差数列相加。
class Solution { public: bool isPerfectSquare(int num) { int x = 1; while(num > 0 ){ num = num - x; x = x + 2; } return num == 0; } };