website/content/ChapterFour/0300~0399/0367.Valid-Perfect-Square.md
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
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
说明:不要使用任何内置的库函数,如 sqrt。
package leetcode
func isPerfectSquare(num int) bool {
low, high := 1, num
for low <= high {
mid := low + (high-low)>>1
if mid*mid == num {
return true
} else if mid*mid < num {
low = mid + 1
} else {
high = mid - 1
}
}
return false
}