go: add «633. Sum of Square Numbers»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
898040fcad
commit
e74d5bfa99
1 changed files with 28 additions and 0 deletions
28
go/sum-of-square-numbers.go
Normal file
28
go/sum-of-square-numbers.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in a new issue