# <LeetCode OJ> 319. Bulb Switcher

Total Accepted: 20000 Total Submissions: 48830 Difficulty: Medium

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.

class Solution {
public:
int bulbSwitch(int n) {
//只要是<=n的平方数，它的最终状态一定是关着的
int result=0;
for(int i=1;i*i<=n;i++)
result++;
return result;
}
};

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

## LeetCode319——Bulb Switcher

• zhangxiao93
• 2015年12月21日 09:58
• 2190

## 319. Bulb Switcher

// CPP class Solution { public: int bulbSwitch(int n) { int result = 0; for (int...
• baidu_23318869
• 2015年12月23日 13:13
• 1845

## leetcode 672. Bulb Switcher II

There is a room with n lights which are turned on initially and 4 buttons on the wall. After perfor...
• huanghanqian
• 2017年09月05日 22:34
• 958

## [leetcode]319. Bulb Switcher

• zorelemn
• 2016年04月13日 20:53
• 127

## LeetCode *** 319. Bulb Switcher

• treeshy
• 2016年04月07日 08:17
• 81

## leetcode 319. Bulb Switcher

QuestionThere are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn of...
• StubbornAnt
• 2015年12月28日 09:56
• 253

## [leetcode] 319. Bulb Switcher

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every...
• TstsUgeg
• 2015年12月19日 19:47
• 127

## LeetCode 319. Bulb Switcher

LeetCode，开灯问题，Bulb Switcher，多解
• Appletable
• 2016年02月20日 09:13
• 130

## 【LeetCode】 319. Bulb Switcher

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
• qq_31129455
• 2017年01月10日 04:48
• 74

## [Leetcode] 319. Bulb Switcher

• xinyuehuixin
• 2016年06月03日 10:33
• 140

举报原因： 您举报文章： 319. Bulb Switcher 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)