# 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  n th 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.
``````

``````class Solution {
public:
int bulbSwitch(int n) {
int res = 1;
while (res * res <= n) ++res;
return res - 1;
}
};
``````

``````class Solution {
public:
int bulbSwitch(int n) {
return sqrt(n);
}
};
``````

Bulb Switcher II

6.6 Toggle Lockers

https://leetcode.com/problems/bulb-switcher/discuss/77104/Math-solution..

https://leetcode.com/problems/bulb-switcher/discuss/77112/Share-my-o(1)-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation