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.

Subscribe to see which companies asked this question

直接看大神的分析,厉害了word哥

A bulb ends up on iff it is switched an odd number of times.

Call them bulb 1 to bulb n. Bulb i is switched in round d if and only if d divides i. So bulb i ends up on if and only if it has an odd number of divisors.

Divisors come in pairs, like i=12 has divisors 1 and 12, 2 and 6, and 3 and 4. Except when i is a square, like 36 has divisors 1 and 36, 2 and 18, 3 and 12, 4 and 9, and double divisor 6. So bulb i ends up on if and only if i is a square.

So just count the square numbers.

Let R = int(sqrt(n)). That's the root of the largest square in the range [1,n]. And 1 is the smallest root. So you have the roots from 1 to R, that's R roots. Which correspond to the R squares. So int(sqrt(n)) is the answer. (C++ does the conversion to int automatically, because of the specified return type).

 public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}

LeetCode 319. Bulb Switcher的更多相关文章

  1. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  2. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  3. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

  4. [LeetCode]319. Bulb Switcher灯泡开关

    智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...

  5. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  6. 319. Bulb Switcher

    题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...

  7. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  8. 319. Bulb Switcher (Math, Pattern)

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  9. 319 Bulb Switcher 灯泡开关

    初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯 ...

随机推荐

  1. Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例

    Ubuntu13.04 Eclipse下编译安装Hadoop插件及使用小例 一.在Eclipse下编译安装Hadoop插件 Hadoop的Eclipse插件现在已经没有二进制版直接提供,只能自己编译. ...

  2. 在MVC中添加异常增加日志

    MVC的结构非常棒,基本你能想到注入的地方都可以找到地方,譬如IActionFilter,IResultFilter,IAuthorizationFilter以及IExceptionFilter 以下 ...

  3. 新的IRepository接口+搜索和排序解耦(+基于Castle实现)

    新的IRepository接口+搜索和排序解耦(+基于Castle实现) 经过了上篇IRepository和IRepository<T>的讨论[文章地址为:http://www.cnblo ...

  4. FpGrowth算法

    FpGrowth算法 频繁项集与关联规则挖掘(2)--FpGrowth算法   上一篇介绍了关联规则挖掘的一些基本概念和经典的Apriori算法,Aprori算法利用频繁集的两个特性,过滤了很多无关的 ...

  5. Mongodb 集群搭建以及常见错误(不分块,分片,以及加验证)

    1 关于Replica Sets +Sharding(主从复制加分片)搭建,不这详细去说,网上有很多,大部分的例子就三台服务器之间做主从复制,分2个shard,架构图如下 mongodb节点 分别为 ...

  6. 用maven来创建web工程

    大约已经一个月没有写博客了,一个是最近太忙,另外啊还是人慢慢的变懒,最近在拆pool,用到了maven,因此呢把自己的一些心得记下来了,供大家参考,欢迎指正. 1.配置maven环境: 首先下载mav ...

  7. 调试PostSharp DEMO 遇到的问题

    我今天在WIN7下的VS2010.VS2012调试PostSharp例子程序,下载的是PostSharp1.5版本,但是就是不进断点啊,我想了想这是怎么回事啊,于是我更换了2.0的再进行调试具然好使了 ...

  8. AS3中释放优化的几条常识

    as3中垃圾和堆弃物如不及时清理,会造成进程的速度方面授予限制,下面讲几点关于释放优化的几条内容. 被删除对象在外部的所有引用一定要被删除干净才能被系统当成垃圾回收处理掉: 父对象内部的子对象被外部其 ...

  9. [oc] instancetype vs id for Objective-C 【转】

    原贴地址:http://blog.csdn.net/lyy_whg/article/details/12846055 http://www.iwangke.me/2013/01/06/instance ...

  10. MVC源码解析 - UrlRoutingModule / 路由注册

    从前面篇章的解析, 其实能看的出来, IHttpModule 可以注册很多个, 而且可以从web.config注册, 可以动态注册. 但是有一个关键性的Module没有讲, 这里就先来讲一下这个关键性 ...