leetcode 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.
题意:
给定n,初始的时候灯都是灭的,让你进行进行开关灯。第一轮的时候把1的倍数反转(原来关就开,原来开就关),第二轮把2的倍数翻转,以此类推到第n轮。
求最后灯打开的个数
思路:
我们来分析下:对于素数,那么它仅有1和它本身,最后一定是关掉的。
对一普通的,一定是关掉的,因子成对出现
对于完全平方数,因为有一个倍数不成对出现,所以一定是打开的。比如4 => 1,4 2
所以本题就是求1~n有几个完全平方数。
那么,怎么求呢?从1开始到n,每个数测试一下?时间复杂度O(n),太慢
正确的是直接sqrt(n),就可以算出来啦~
C++ 1
2
3
4
5
6class Solution {
public:
int bulbSwitch(int n) {
return sqrt(n);
}
};
Python 1
2
3
4
5
6
7class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
"""
return int(math.sqrt(n))
Java 1
2
3
4
5public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}