Leetcode Bulb Switcher
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 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.
如果直接按照题目给出的思路来解答,会导致超时。
观察后可以看出一下的过程,假设我们观察第9个灯泡,这个灯泡会在第1, 3, 9轮中分别被toggle一次,所以最后会保持点亮。第10个灯泡,会在1,2,5,10轮被toggle四次,最后保持熄灭。
所以如果一个数的因子个数是奇数个,那么这个灯泡最后就是点亮的。反之,这个灯泡最后就是熄灭的。显然之后完全平方数才有奇数个因子,因为因子都是成对儿出现的。
所以这个问题其实就是问,<=n的正整数中有多少个完全平方数。也就是<= sqrt(n) 的最大正整数是哪个?
return int(math.sqrt(n))
Leetcode Bulb Switcher的更多相关文章
- [LeetCode] Bulb Switcher 灯泡开关
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- LeetCode Bulb Switcher 319
变换灯泡颜色 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- LC 672. Bulb Switcher II
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- LeetCode 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- leetcode【67】-Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
随机推荐
- 针对苹果最新审核要求为应用兼容IPv6
在WWDC2015上苹果宣布iOS9将支持纯IPv6的网络服务.2016年初开始所有提交到App Store的应用必须支持IPv6.为确保现有的应用是兼容的,我们需要注意下面几点. 不建议使用底层的网 ...
- There is no ‘Animation’ attached to the “Player” game object
There is no ‘Animation’ attached to the “Player” game object 在照着龚老师的Unity3D投篮游戏视频教程练习时,遇到这个错误提示. 我知道 ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- font和lineheight冲突。
font:14px bold arial; line-height:40px; 这样写font的话line-height不会有效,只要把font拆分写就有效,chrome ie ff下都是.
- 这些javascript面试题,你做对了几道?
1.---------------------------------------------------- var fun = function(){ this.name = 'peter'; re ...
- SELECT (Transact-SQL)
从数据库中检索行,并允许从 SQL Server 中的一个或多个表中选择一个或多个行或列. 虽然 SELECT 语句的完整语法较复杂,但其主要子句可归纳如下: [ WITH <common_t ...
- 将DBF文件导入Sqlserver数据库
项目中的问题:用户选择N个dbf文件导入sql2005数据库,由于每年dbf表结构都在变化,所以在sql2005中根本就不存在,需要每年根据dbf的结构自动建表.(文章来自http://blog.cs ...
- [转]Linux系统中‘dmesg’命令处理故障和收集系统信息的7种用法
'dmesg'命令显示linux内核的环形缓冲区信息,我们可以从中获得诸如系统架构.cpu.挂载的硬件,RAM等多个运行级别的大量的系统信息.当计算机启动时,系统内核(操作系统的核心部分)将会被加载到 ...
- (译文)MVC通用仓储类
Generic Repository Pattern MVC Generic Repository Pattern MVC 原文链接:http://www.codeproject.com/Articl ...
- js 0.1+0.2!=0.3
准确的说就是js小数采用ieee的64位的双精度,1位表示正负,11位指数,52位小数,所以对于0.1js是无法精确表示的的,所以会多点, http://www.jb51.net/article/77 ...