题目描述:
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: True
Example 2:
Input: 14
Returns: False
本题属于数学分类。
思路:
任意完全平方数可通过1+3+5...+K的和得到。
实现代码:
public class Solution {
public bool IsPerfectSquare(int num) {
var start = 1;
while(num > 0){
num -= start;
start += 2;
}
return num == 0;
}
}