题目
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~num中找平方等于num的值i,找到返回true
- 如果i2大于num,说明num不是平方数,返回false
代码
func isPerfectSquare(num int) bool {
for i := 1; i <= num; i++ {
if num == i * i {
return true
} else if num < i * i {
return false
}
}
return false
}