1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/go/sum-of-square-numbers.go
Matej Focko e74d5bfa99
go: add «633. Sum of Square Numbers»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-18 12:10:01 +02:00

28 lines
436 B
Go

package main
func judgeSquareSum(c int) bool {
bsearch := func(left, right, target int64) bool {
for left <= right {
mid := left + (right-left)/2
if mid*mid == target {
return true
} else if mid*mid > target {
right = mid - 1
} else {
left = mid + 1
}
}
return false
}
t := int64(c)
for a := int64(0); a*a <= t; a++ {
b := t - a*a
if bsearch(0, b, b) {
return true
}
}
return false
}