leetcode update

This commit is contained in:
gameloader 2024-06-17 14:58:43 +08:00
parent e6f40afb46
commit f82868073e

View File

@ -7523,3 +7523,39 @@ func minPatches(nums []int, n int) int {
return result return result
} }
``` ```
## day112 2024-06-17
### 633. Sum of Square Numbers
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
![0617sYGtK80QtKIS](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0617sYGtK80QtKIS.png)
### 题解
本题计算出c的平方根, 用两个变量low和high分别标记0和c的平方根(取下整), 计算两变量的平方和, 若比c大则减小high, 否则增大low, 直到得到平方和为c或者low=high为止.
### 代码
```go
func judgeSquareSum(c int) bool {
high := int(math.Sqrt(float64(c)))
low := 0
for low <= high{
if low*low + high*high == c{
return true
}else if low*low + high*high > c{
high--
}else{
low++
}
}
return false
}
```
### 总结
本题还有一些有趣的相关数学知识, 费马两平方和定理和两数平方和定理, 前者说明了一个奇素数何时能被写为两平方数之和. 满足条件的奇素数也被称为毕达哥拉斯质数, 后者给出了所有大于1的整数在什么情况下能被写为两平方数之和. 是前者的推广. 可参考
[Fermat's theorem on sums of two squares](https://en.wikipedia.org/wiki/Fermat%27s_theorem_on_sums_of_two_squares)
[Sum of two squares theorem](https://en.wikipedia.org/wiki/Sum_of_two_squares_theorem)