当前位置:网站首页>2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
2021-05-04: given a non negative integer C, you need to judge whether there are two integers a and B, so that a*a+b*b=c.
2022-06-24 15:52:00 【Fuda scaffold constructor's daily question】
2021-05-04: Given a nonnegative integer c, You have to decide if there are two integers a and b, bring a_a+b_b=c.【 give an example 】c=5 when , return true.c=4 when , return true.c=3 when , return false.
Fuda answer 2021-05-04:
The sum of four squares Theorem . Time complexity :O(sqrt(N)). Spatial complexity :O(1).
1.n Always divide by 4, Until it's not divisible .
2.n%8, If yu 7, Go straight back to 4.
3. from 1 To sqrt(n) Start the cycle ,a_a+b_b=c When established ,a and b Not for 0, return 2;a and b There is one for 0, return 1.
4. return 3.
5. In the subject , The return value is 1 and 2 Yes. true. The return value is 3 and 4 Yes. false.
The code to use golang To write . The code is as follows :
package main
import (
"fmt"
"math"
)
func main() {
for i := 1000; i <= 1100; i++ {
ret := isSquares(i)
fmt.Println(i, ret)
}
}
//4+1+1+1
func isSquares(n int) bool {
return numSquares(n) <= 2
}
func numSquares(n int) int {
for n&3 == 0 { //n%4==0
n >>= 2 //n/=4
}
if n&7 == 7 { //n%8==7
return 4
}
a := 0
for a*a <= n {
b := int(math.Sqrt(float64(n - a*a)))
if a > b {
break
}
if a*a+b*b == n {
ret := 0
if a != 0 {
ret++
}
if b != 0 {
ret++
}
return ret
}
a += 1
}
return 3
}The results are as follows :
边栏推荐
- 如何扩展aws主机上的磁盘空间
- Precautions for using JMeter suite to build a pressure test environment
- Easynvr has been connected to the third-party supervision platform. How to achieve local Internet access
- Mongodb introductory practical tutorial: learning summary directory
- Reference to junit5 test framework in gradle
- 手机同花顺股票开户安全吗!
- 【Prometheus】4. Monitoring cases
- Decomposition of Uber dependency injection into dig source code analysis
- Leetcode 139. Mot break word Split (medium)
- Database tools in intelij can connect but cannot display schema, tables
猜你喜欢

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

推荐几款超级实用的数据分析利器

刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪

Logging is not as simple as you think

Apple is no match for the longest selling mobile phone made in China, and has finally brought back the face of the domestic mobile phone

Implement Domain Driven Design - use ABP framework - domain logic & application logic

设备通过国标GB28181接入EasyCVR平台,出现断流情况该如何解决?
![[C language questions -- leetcode 12 questions] take you off and fly into the garbage](/img/ca/a356a867f3b7ef2814080fb76b9bfb.png)
[C language questions -- leetcode 12 questions] take you off and fly into the garbage

一文详解JackSon配置信息

【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆
随机推荐
How to efficiently transfer enterprise business data?
MySQL development specification
One article explains Jackson configuration information in detail
一文详解JackSon配置信息
Network engineers must know the network essence knowledge!
A full set of tutorials for interviewers from Android manufacturers teach you: prepare for the interview and get the offer smoothly!
如何在Thymeleaf3标签中使用嵌套标签
Wi-Fi 7 来啦,它到底有多强?
国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
Precautions for using JMeter suite to build a pressure test environment
Tencent cloud native intelligent data Lake Conference will be held, revealing the panoramic matrix of Tencent cloud data Lake products for the first time
QoS Technology in network
Install the imagemagick7.1 library and the imageick extension for PHP
Here comes Wi Fi 7. How strong is it?
At? Let's blow the air conditioner together!
Parameterized tests guide in junit5
Istio FAQ: return 426 status code
Paper: Google TPU
Motion planning of floating base robot