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 不...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

319. Bulb Switcher

319. Bulb Switcher

LeetCode 319. Bulb Switcher 解题报告

LeetCode 319. Bulb Switcher 解题报告

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

[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...

[Leetcode] 319. Bulb Switcher 解题报告

题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn o...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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