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. GitHub删除文件

    删除单个文件 一般情况下,通常可直接在文件管理器中直接把文件删了,或者用rm命令删了 $ rm README.txt 这个时候,工作区是删除了,可版本库还没有.git status命令会立刻告诉你哪些 ...

  2. cocos2dx在Eclipse下编译报错:Cannot find module with tag 'CocosDenshion/android' in import path

    在Eclipse下编译cocos2dx项目,报错如下: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/ ...

  3. 添加可运行的js代码

    如何在博客园的文章/随笔中添加可运行的js代码 在博客园浏览大牛们写的文章时,经常会看到在文章中混有一些可运行示例,例如司徒正美的博客中: 带有可运行示例 可以点击“运行代码” 经过一番小小的探索,掌 ...

  4. Arduino 各种模块篇 蓝牙模块 手机蓝牙控制Arduino LED灯

    解决方案. 条件: 1.手机android 商店下载 blueTerm 2.向arduino中载入如下代码: char val; ; void setup() { Serial.begin(); pi ...

  5. 32位Win7下安装与配置PHP环境(二)

    本安装实例中用到的三个软件,都可以直接从官网下载,为了方便,也可以直接从本人的CSDN资源中打包下载. 三个安装文件如图示: CSDN高速下载地址: http://download.csdn.net/ ...

  6. Linq 更改主键值

    有一个班级表,主键是class_id,在管理班级时要进行逻辑删除,而只是单纯的is_del字段(记录每条数据是否有效)更改为true,主键class_id如果不变动,在再次增加一个班级时,其主键如果和 ...

  7. Data Guard组件等相关介绍

    1.Data Guard组件介绍  Data Guard架构归类为3个主要的组件. Data Guard 重做传输服务  重做传输服务用来将主数据库生成的重做数据传输给备用数据库. Data Guar ...

  8. Nutz中过滤特殊字符

    ##Servlet中有获取Request参数的方法,而Nutz中也有重写类似的方法,我们只要知道它如何得到RequestMap就可以处理请求中的参数,进而对它进行处理. 在Nutz项目中的MainMo ...

  9. [asp.net] 利用WebClient上传图片到远程服务

    一.客户端 1.页面 1 <form id="Form1" method="post" runat="server" enctype= ...

  10. [HMLY]10.深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用

    前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如Retain Circle的问题. 目录 1.Retain ...