题目:

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.

链接:  https://leetcode.com/problems/bulb-switcher/

题解:

类似智力题。划一下5个灯泡的流程图就会发现最后亮的是1号和4号灯泡。最后结果就是求在n里包含多少个完全平方数。 在Discuss里Stefan Pochmann写得很好,放在reference里。

Time Complexity - O(1),  Space Complexity - O(1)。

public class Solution {
public int bulbSwitch(int n) {
if (n < 1) {
return 0;
}
return (int)Math.sqrt(n);
}
}

二刷:

Java:

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

Reference:

https://leetcode.com/discuss/75014/math-solution

319. Bulb Switcher的更多相关文章

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

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

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

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

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

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

  4. Java [Leetcode 319]Bulb Switcher

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

  5. LeetCode 319. Bulb Switcher

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

  6. 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 ...

  7. 319 Bulb Switcher 灯泡开关

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

  8. Leetcode 319 Bulb Switcher 找规律

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

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

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

随机推荐

  1. Javascript日常编码中的一些常见问题

    一.尽量少用全局变量   这是一个疑问最少,同时流传最 广的一条.Javascript使用函数管理作用域,全局变量最大的问题在于同名变量冲突.这种隐患产生比较直接的两个原因就是Javascript语言 ...

  2. bzoj4337: BJOI2015 树的同构 树哈希判同构

    题目链接 bzoj4337: BJOI2015 树的同构 题解 树哈希的一种方法 对于每各节点的哈希值为hash[x] = hash[sonk[x]] * p[k]; p为素数表 代码 #includ ...

  3. [BZOJ3451][Tyvj1953]Normal(点分治+FFT)

    https://www.cnblogs.com/GXZlegend/p/8611948.html #include<cmath> #include<cstdio> #inclu ...

  4. Python3函数式编程

    Python函数式编程 函数式编程可以使代码更加简洁,易于理解.Python提供的常见函数式编程方法如下: map(函数,可迭代式)映射函数 filter(函数,可迭代式)过滤函数 reduce(函数 ...

  5. [USACO09JAN]Total Flow

    OJ题号: BZOJ3996.洛谷2936.SPOJ-MTOTALF.SCU3353 思路: 题目的要求是将所有边合并成一条边,求合并后的流量. 实际上相当于直接求最大流. EdmondsKarp模板 ...

  6. ParallaxNode视差节点实现远景近景的不同层次移动

    Cocos2d-x有ParallaxNode视差节点,视察顾名思义,就是造成不同的移动速率的效果. 我想大家都玩过刀塔传奇,他的背景有远景和近景之分,而且你滑动屏幕的时候远景和近景是按照不同的速率移动 ...

  7. vs配置TFS

  8. python: 序列化/反序列化及对象的深拷贝/浅拷贝

    一.序列化/反序列化 python中内置了很多序列化/反序列化的方式,最常用的有json.pickle.marshal这三种,示例用法如下: import json import pickle imp ...

  9. keybd_event 被 SendInput 替代

    keybd_event   函数功能:该函数合成一次击键事件.系统可使用这种合成的击键事件来产生WM_KEYUP或WM_KEYDOWN消息,键盘驱动程序的中断处理程序调用keybd_event函数.在 ...

  10. Git -- 本地 一个相同的新的分支 并 推送到远程仓库

    (一).创建本地分支 git checkout -b 新分支名 执行该指令后,会在本地创建一个新分支,该分支是从当前分支上检出的,所以所有文件内容都和当前分支一模一样,这是正常的.创建成功后,将自动切 ...