263. Ugly Number
class Solution:
def isUgly(self, num):
\"\"\"
:type num: int
:rtype: bool
\"\"\"
if num<=0:
return False
while num%2==0:
num//=2
while num%3==0:
num//=3
while num%5==0:
num//=5
return True if num==1 else False
264. Ugly Number II
class Solution:
def nthUglyNumber(self, n):
\"\"\"
:type n: int
:rtype: int
\"\"\"
if n<0:
return 0
dp=[1]*n
i1=i2=i3=0
for i in range(1,n):
dp[i]=min(2*dp[i1],3*dp[i2],5*dp[i3])
if dp[i]==2*dp[i1]: i1+=1
if dp[i]==3*dp[i2]: i2+=1
if dp[i]==5*dp[i3]: i3+=1
return dp[n-1]
313. Super Ugly Number
class Solution( ):
def nthSuperUglyNumber(self, n, primes):
\"\"\"
:type n: int
:type primes: List[int]
:rtype: int
\"\"\"
if n<=0:
return
dp=[1]*n
index=[0]*len(primes)
for i in range(1,n):
dp[i]=min([dp[index[j]]*primes[j] for j in range(len(primes))])
for j in range(len(primes)):
if dp[index[j]]*primes[j]==dp[i]:
index[j]+=1
return dp[n-1]
继续阅读与本文标签相同的文章
上一篇 :
地平线旭日二代发布:做芯片只是过渡,不是终局
-
基于宜搭的“报表分析”实践案例
2026-05-18栏目: 教程
-
javascript教程:实现函数柯里化与反柯里化
2026-05-18栏目: 教程
-
基于宜搭的“定时消息通知”实践案例
2026-05-18栏目: 教程
-
AIoT入门:用虚拟设备体验物联网平台设备上云&设备数据存储
2026-05-18栏目: 教程
-
基于宜搭的“企业报销流程”实践案例
2026-05-18栏目: 教程
