Python | Leetcode Python题解之第279题完全平方数
🕗 发布于 2024-07-26 03:46 Python Leetcode 题解
题目:
题解:
class Solution {
public:
// 判断是否为完全平方数
bool isPerfectSquare(int x) {
int y = sqrt(x);
return y * y == x;
}
// 判断是否能表示为 4^k*(8m+7)
bool checkAnswer4(int x) {
while (x % 4 == 0) {
x /= 4;
}
return x % 8 == 7;
}
int numSquares(int n) {
if (isPerfectSquare(n)) {
return 1;
}
if (checkAnswer4(n)) {
return 4;
}
for (int i = 1; i * i <= n; i++) {
int j = n - i * i;
if (isPerfectSquare(j)) {
return 2;
}
}
return 3;
}
};
原文地址:https://blog.csdn.net/Mopes__/article/details/140651269
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!
-
纯HTML实现标签页切换
ul><li><a>#tab1<div>:target总结: 使用纯HTML和CSS实现标签页切换是一种简洁有效的方法,特别适合简单的内容展示需求。它提供了良
阅读更多2024-10-18
-
MySQL中的增查操作:探索数据的奥秘,开启数据之门
在MySQL中,增加数据可以通过INSERT INTO语句来实现。INSERT INTO语句可以将数据插入到表中的一行或多行中。可以通过指定列名和相应的值来插入数据,也可以省略列名,但必须按照表的列顺
阅读更多2024-10-18
-
鸿蒙网络编程系列3-TCP客户端通讯示例
TCP协议是传输层最重要的协议,提供了可靠、有序的数据传输,是多个广泛使用的表示层协议的运行基础,相对于UDP来说,TCP需要经过三次握手后才能建立连接,建立连接后才能进行数据传输,所以效率差了一些,
阅读更多2024-10-18
-
群晖前面加了雷池社区版,安装失败,然后无法识别出用户真实访问IP
有nas的相信对公网都不模式,在现在基础上传带宽能有100兆的时代,有公网代表着家里有一个小服务器,像百度网盘,优酷这种在线服务都能部署为私有化服务。但现在运营商几乎不可能提供公网ip,要么自己买个云
阅读更多2024-10-18
-
Day11-数据库服务主从扩展
1、数据库过滤复制实践2、数据库半同步复制实践 (数据库同步一致性)3、数据库GTID复制实践 (GTID方式实现主从)4、数据库克隆复制实践5、数据库多源复制实践 (将多个主库的数据
阅读更多2024-10-18
-
CTFHUB技能树之SQL——MySQL结构
1 and 1=2 union select 1,2#或者-1 union select 1,2#都行。没有发现数据,猜测数据可能不存在数据库中,修改注入语句。知道字段数量为2后,可以查看数据库位置。
阅读更多2024-10-18
-
关于编程小白如何成为大神?
爱我的我爱的都不能选,应该是我爱她她也爱我的(这个相对比较正确,但是很难的,现在我看到女孩,有的时候,就觉得这个女孩很好看,妈的!我的零碎时间都是用来进行单词的背诵, 也是在不断地背单词中,我找到了自
阅读更多2024-10-18
-
【Linux线程】Linux多线程编程:深入理解线程互斥与同步机制
在当今这个数据驱动、高性能计算盛行的时代,多线程编程已经成为软件开发中不可或缺的一部分。Linux,作为开源世界的领头羊,其强大的多线程支持为开发者提供了广阔的舞台,让高并发、高性能的应用得以实现。然
阅读更多2024-10-18
-
从零开始实现大语言模型(十二):文本生成策略
本文介绍大语言模型`GPTModel`预测生成连续自然语言文本的流程,以及4种从概率分布中选择下一个token的策略,并实现文本生成函数`generate_text`。
阅读更多2024-10-18
-
modbus poll监控抗凝秤通信err提示有crc校验错误等原因
一、CRC错误原因及解决方法CRC(循环冗余校验)错误是数据传输过程中常见的错误类型之一,可能由以下原因引起:噪声干扰:在数据传输过程中,噪声干扰可能导致数据位的改变,从而引发CRC校验错误。通过波形
阅读更多2024-10-18