319.灯泡开关
最后更新于
class Solution:
def bulbSwitch(self, n: int) -> int:
res = 0
for i in range(1, n+1):
cnt = 0
j=1
while j * j <= i:
if i % j == 0:
cnt += 1
if j * j != i:
cnt += 1
j += 1
if cnt % 2:
res += 1
return resclass Solution:
def bulbSwitch(self, n: int) -> int:
res = 0
for i in range(1, n+1):
j = int(sqrt(i))
if j ** 2 == i: #j是i的平方根
res += 1
return resclass Solution:
def bulbSwitch(self, n: int) -> int:
return int(sqrt(n + 0.5))