website/content/ChapterFour/0600~0699/0633.Sum-of-Square-Numbers.md
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a^2 + b^2 = c.
Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c。
low * low + high * high 和 c 是否相等。从 [0, sqrt(n)] 区间内进行二分,若能找到则返回 true,找不到就返回 false 。
package leetcode
import "math"
func judgeSquareSum(c int) bool {
low, high := 0, int(math.Sqrt(float64(c)))
for low <= high {
if low*low+high*high < c {
low++
} else if low*low+high*high > c {
high--
} else {
return true
}
}
return false
}