319. Bulb Switcher

原创 2016年08月29日 16:52:36

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.
思路:

初始状态,所有的灯泡都是熄灭的,最终的状态应该是他们翻转的次数,如果是奇数,就是亮,如果是偶数就是熄灭。

所以,检查所有数的乘积因子,除了平方之外,都是偶数个,所以只有平方数的灯泡是亮着的。

	public static int bulbSwitch(int n) {
		return (int)(n >= 0 ? Math.sqrt(n) : 0);
	}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 319 Bulb Switcher

leetcode319 Bulb Switcher题目描述 There are n bulbs that are initially off. You first turn on all the ...

Leetcode 319. Bulb Switcher

Bulb Switcher好久没更新博客啦,主要是因为都没刷到什么好题,这一篇也是大大的水题,不过因为思考的过程种发生了种种有(yu)趣(chun)的故事,姑且把它当作一篇娱乐向的博文来写233 不...

319. Bulb Switcher

319. Bulb Switcher

LeetCode 319. Bulb Switcher 解题报告

LeetCode 319. Bulb Switcher 解题报告

LeetCode 319 Bulb Switcher(灯泡切换)(从规律中发现算法……)

翻译这儿有n个灯泡被初始化。首先,你点亮所有的灯泡。然后,你关掉每第二个灯泡。在第三回,你切换每第三个灯泡(如果它是关闭的则开启它,如果它是开启的则关闭它)。对于第i回,你切换每第i个灯泡。对于第n回...
  • NoMasp
  • NoMasp
  • 2016年02月29日 22:10
  • 2827

【leetcode】319. Bulb Switcher

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

LeetCode319——Bulb Switcher

这是一道智商碾压的题。。。

LeetCode #319 - Bulb Switcher - Medium

ProblemThere are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn of...
  • Arcome
  • Arcome
  • 2016年12月07日 20:34
  • 218

[Leetcode] 319. Bulb Switcher

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

<LeetCode OJ> 319. Bulb Switcher

Total Accepted: 20000 Total Submissions: 48830 Difficulty: Medium There are n bulbs that are in...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:319. Bulb Switcher
举报原因:
原因补充:

(最多只允许输入30个字)