当前位置:网站首页>The third divisor of leetcode simple question
The third divisor of leetcode simple question
2022-04-23 08:14:00 【·Starry Sea】
subject
Give you an integer n . If n There are exactly three positive divisors , return true ; otherwise , return false .
If there are integers k , Satisfy n = k * m , So integers m Namely n One of the Divisor .
Example 1:
Input :n = 2
Output :false
explain :2 There are only two divisors :1 and 2 .
Example 2:
Input :n = 4
Output :true
explain :4 There are three divisors :1、2 and 4 .
Tips :
1 <= n <= 10^4
source : Power button (LeetCode)
Their thinking
First of all, a number must have more than two divisors, which is 1 And itself , We can set up a flag when 2 and n-1 Found a divisor between , So conversion tag , If the divisor is encountered again, the check mark can return the result , In addition, if you don't encounter divisor , It shows that this number is a prime number and can also return the result .
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
Actually, a number with three divisors , One is 1 The other is itself , There is also a pair of divisors that must be equal . But finding this logarithm must start from 2 Start looking for , This is not biased .
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
版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590377.html
边栏推荐
- 有意思的js 代码
- The following program deletes n consecutive words starting from the ith character from the string str
- Quick rehearsal exercise
- [programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
- LeetCode简单题之三除数
- AAAI 2022 recruit speakers!!
- Find the largest of 3 strings (no more than 20 characters per string).
- 数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
- 在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)
- 常用正则表达式
猜你喜欢
随机推荐
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
How does feign integrate hystrix
浅谈ES6尾调优化
利用Js实现一个千分位
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
Compiler des questions de principe - avec des réponses
三星,再次“西征”
3C装配中的机械臂运动规划
使用 Ingress 实现金丝雀发布
Asynchronous learning
多目视觉SLAM
Alibaba sentinel learning QA
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
yum源仓库本地搭建的两种方法
搜一下导航完整程序源码
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
谈谈那些基础但不简单的股票数据
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Jetson Xavier NX (3) bazel mediapipe installation
[problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened