【LeetCode】319. Bulb Switcher 解题报告(Python)
2024-09-05 08:14:55
【LeetCode】319. Bulb Switcher 解题报告(Python)
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/bulb-switcher/description/
题目描述:
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
Example:
Given n = 3.
At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].
So you should return 1, because there is only one bulb is on.
题目大意
n个灯泡,起始状态是灭的,需要按照1的倍数,2的倍数,3的倍数……的位置打开关闭n次,求最后亮了几个。
解题方法
真的是智力题,暴力解法超时,最后的结果是开平方就行。
对于这个题,在http://blog.csdn.net/baidu_23318869/article/details/50386323上有详细的解释,应该能看懂。就不多说了。
代码:
class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
"""
return int(math.sqrt(n))
日期
2018 年 3 月 5 日
最新文章
- 封装系统(以封装Windows 7为例)
- a letter and a number
- redis 安装及配置
- C# static 干货全解析
- IOS 开展 分别制定了iphone 和 ipad 好? 或开发一个 Universal好?
- nginx 引入外部文件
- html_web存储
- js 常用正则表达式表单验证代码
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
- 使用筛选器特性标记方法解决webapi 跨域问题
- MYSQL的group by笔记
- git ignore 总结
- python--第十七天总结(Django)
- java中的Sort函数,你值得看
- [日常] 编写HTTP接口文档
- C语言位域——精妙使用内存
- Android APP分享功能实现
- [hihoCoder] 骨牌覆盖问题·二
- 本地Jdev Run PG报严重: Socket accept failed错误
- 机器学习实战笔记(Python实现)-07-模型评估与分类性能度量