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);
	}



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

LeetCode319——Bulb Switcher

这是一道智商碾压的题。。。
  • zhangxiao93
  • zhangxiao93
  • 2015年12月21日 09:58
  • 2195

319. Bulb Switcher

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

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
  • huanghanqian
  • 2017年09月05日 22:34
  • 965

[leetcode]319. Bulb Switcher

考虑第n个灯的最终状态。显然,在第i轮,只要i是n的因子,包括n,都会改变第n个bulb的状态。当bulb的改变次数(因子个数)为偶数时,灯为开,否则,灯为灭。另外考虑n的一个因子p,对应的另一个因子...
  • zorelemn
  • zorelemn
  • 2016年04月13日 20:53
  • 127

LeetCode *** 319. Bulb Switcher

题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off e...
  • treeshy
  • 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
  • StubbornAnt
  • 2015年12月28日 09:56
  • 253

Bulb Switcher II问题及解法

672. Bulb Switcher II LeetCode
  • u011809767
  • u011809767
  • 2017年09月13日 09:10
  • 318

[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
  • TstsUgeg
  • 2015年12月19日 19:47
  • 127

319. Bulb Switcher && 672. Bulb Switcher II

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
  • zjucor
  • zjucor
  • 2017年09月03日 13:23
  • 825

[Leetcode] 319. Bulb Switcher

题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
  • xinyuehuixin
  • xinyuehuixin
  • 2016年06月03日 10:33
  • 140
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:319. Bulb Switcher
举报原因:
原因补充:

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