当前位置:网站首页>LeetCode简单题之三除数
LeetCode简单题之三除数
2022-04-23 07:02:00 【·星辰大海】
题目
给你一个整数 n 。如果 n 恰好有三个正除数 ,返回 true ;否则,返回 false 。
如果存在整数 k ,满足 n = k * m ,那么整数 m 就是 n 的一个 除数 。
示例 1:
输入:n = 2
输出:false
解释:2 只有两个除数:1 和 2 。
示例 2:
输入:n = 4
输出:true
解释:4 有三个除数:1、2 和 4 。
提示:
1 <= n <= 10^4
来源:力扣(LeetCode)
解题思路
首先一个数肯定有超过两个除数就是1和它本身,我们可以设立一个标记当在2和n-1之间发现了一个除数,那么标记转换,如果再遇到除数则检查标记就能返回结果,另外如果没有遇到除数,说明此数是一个质数也可以返回结果了。
class Solution:
def isThree(self, n: int) -> bool:
if n==2:
return False
flag=False
for i in range(2,n):
if n%i==0:
if flag:
return False
flag=True
return flag

其实拥有三个除数的数,一个是1另一个是其本身,还有一个必定是相等的一对除数。但是找这一对数必须从2开始找起,这样才不是有偏的。
class Solution:
def isThree(self, n: int) -> bool:
for i in range(2,int(n**0.5)+1):
if n%i==0:
return i**2==n
return False

版权声明
本文为[·星辰大海]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_18560985/article/details/124357105
边栏推荐
- User manual of Chinese version of solidity ide Remix
- 学fpga(从verilog到hls)
- LeetCode 1611. 使整数变为 0 的最少操作次数
- 输入 “ net start mysql ”,出现 “ 发生系统错误 5。 拒绝访问 ” 。问题详解
- [Effective Go 中文翻译]函数篇
- Intranet penetration series: pingtunnel of Intranet tunnel
- 干货!以点为形:可微分的泊松求解器
- 云计算赛项--2020年赛题基础部分[任务3]
- How does feign integrate hystrix
- Cloud computing skills competition -- the first part of openstack private cloud environment
猜你喜欢

一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载

CSV Column Extract列提取

一篇文章看懂变量提升(hoisting)

Cloud computing skills competition -- the first part of openstack private cloud environment

thinkphp6+jwt 实现登录验证
![[untitled]](/img/bb/213d95b60651dfeadb239a70507506.png)
[untitled]

Why are there 1px problems? How?

Buuctf misc brush questions

校园转转二手市场源码下载
![[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test](/img/4a/c741ec4f9aa724e150a5ae24d0f9e9.png)
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
随机推荐
惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧
以下程序实现从字符串str中删除第i个字符开始的连续n个字
Intranet penetration series: ICMP of Intranet tunnel_ Tran
Ubuntu安装Mysql并查询平均成绩
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
【Appium】测试时遇到手机内嵌H5页面的切换问题
LeetCode 1611. 使整数变为 0 的最少操作次数
DVWA靶场练习
Positioning and decoration style
The following program deletes n consecutive words starting from the ith character from the string str
Briefly describe the hierarchical strategy of memory
Intranet penetration series: dns2tcp of Intranet tunnel
NFT ecological development of Ignis public chain: unicorn Donation and development of Art
MySQL——第一章节(MySQL中的数据类型)
Implementation of promise all
学fpga(从verilog到hls)
华硕笔记本电脑重装系统后不能读取usb,不能上网
Go语学习笔记 - 结构体 | 从零开始Go语言
[problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened
CSV Column Extract列提取