Leetcode 319. Bulb Switcher

这是一个非常巧妙的题目,网站称之为brain teaser,意思就是代码写起来会非常简介(就是逗你玩:)),题目如下:

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.

第一次: 1×1 只有这一种情况,所以所有的灯泡由off --> on第二次:2×1 1*2也就是说会有两个switch,一个是1为间隔的switch,一个是2为间隔的switch,off->on-->off第三次:如前面所示,1×3 3×1,off-->on-->off,,,,,

以此类推,我们发现a*b总是有b*a相对应,也就是说,有偶数个switch,这样的话,off还是off,on还是on,也就是保持不变,除了一种情况,就是3×3这种,如果sqrt(间隔)为整数,则表示有奇数次switch,会发生状态的变化,那么我们现在再考虑这个问题就很简单了,因为一开始所有的灯泡都是灭的,所以我们只需要寻找满足1.小于n,2.且sqrt为整数的个数。答案显然是sqrt(n)

Code:
class Solution {
public:
    int bulbSwitch(int n) {
        return (int)sqrt(n);
    }
};
 
时间: 2024-10-11 12:49:06

Leetcode 319. Bulb Switcher的相关文章

Leetcode 319 Bulb Switcher 找规律

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

[LeetCode][JavaScript]Bulb Switcher

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

319. 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 ith round, you toggle eve

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 ro

319 Bulb Switcher 灯泡开关

初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯泡切换一次开关. 对于第 n 轮,你只切换最后一个灯泡的开关. 找出 n 轮后有多少个亮着的灯泡.示例:给定 n = 3.状态off表示灯泡关闭,on表示开启.初始时, 灯泡状态 [off, off, off].第一轮后, 灯泡状态 [on, on, on].第二轮后, 灯泡状态 [on, 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 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

【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 toggl

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 th

Bulb Switcher (leetcode java)

问题描述: 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 tog