题目:

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. python魔法方法-反射运算和增量运算

    反射运算 什么是反射运算符,其实就是反转了两个对象,下面先看一个普通运行符的实现: class Foo(object): def __init__(self, x): self.x = x def _ ...

  2. Java并发程序设计(十)设计模式与并发之Future模式

    设计模式与并发之Future模式 核心思想:异步调用. /** * @author: Tang Jiujia * @version: 2017/9/18 0018 15:22 */ public in ...

  3. PHPMailer 使用方法(支持群发):

    一.下载函数包 地址:https://github.com/PHPMailer/PHPMailer 二.测试服务器环境 通过phpinfo()函数打印出是否支持Sockets(Socket属于php的 ...

  4. U3D面试五

    U3D面试题 配置Unity3D调试环境 Visual Studio Tools for Unity 访问http://unityvs.com 安装对应的版本 使用方法(生成项目文件,如何调试) Ar ...

  5. Python:内置函数

    Python所有的内置函数     Built-in Functions     abs() divmod() input() open() staticmethod() all() enumerat ...

  6. Linux--信号阻塞与屏蔽

    1. sigprocmask函数提供屏蔽和解除屏蔽信号的功能. 从而实现关键代码的运行不被打断. 函数声明如下: int sigprocmask(int how, const sigset_t *se ...

  7. CNC系统的多任务并行处理

    (1) CNC系统的多任务性.CNC系统通常作为一个独立的过程控制单元用于工业自动化生产中,因此它的系统软件必须完成管理和控制两大任务.系统的管理部分包括输入.I/O处理.显示和诊断.系统的控制部分包 ...

  8. libnids关于计算校验和引起的抓不到包的现象的解决方法

    libnids关于计算校验和引起的抓不到包的现象的解决方法: nids.h中有这么一段: struct nids_chksum_ctl { u_int netaddr; u_int mask; u_i ...

  9. tmux分屏幕

    1. tmux  a  -t  fly 连接上tmux 2. 左右分屏幕,ctrl+a ,再按% 上下分屏: ctrl+a, 再按“ 切换屏幕: ctrl+a, 再按o 关闭终端: ctrl+a, 再 ...

  10. 一起来给iOS 11找bug: 苹果还是乔布斯时代的细节控吗?

    众所周知,前几天苹果在位于苹果公园的Steve Jobs剧院召开了一年一度的新品发布会,正式揭幕了全屏的iPhoneX, 随后又把iOS 11推送给了测试员(Beta Tester)(正式版将于几周后 ...