LeetCode #319 Bulb Switcher 灯泡开关

319 Bulb Switcher 灯泡开关

Description:
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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Input: 3
Output: 1
Explanation:
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 轮,每两个灯泡你关闭一次。 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭)。第 i 轮,每 i 个灯泡切换一次开关。 对于第 n 轮,你只切换最后一个灯泡的开关。 找出 n 轮后有多少个亮着的灯泡。

示例 :

输入: 3
输出: 1
解释:
初始时, 灯泡状态 [关闭, 关闭, 关闭].
第一轮后, 灯泡状态 [开启, 开启, 开启].
第二轮后, 灯泡状态 [开启, 关闭, 开启].
第三轮后, 灯泡状态 [开启, 关闭, 关闭].

你应该返回 1,因为只有一个灯泡还亮着。

思路:

需要找到 1-n中有几个奇数个因子的数
比如 4, 4的因子为 1, 2, 4, 操作开关只有 3次, 所以最后会打开
所以只要找到 n的平方根即可
时间复杂度O(1), 空间复杂度O(1)

代码:
C++:

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

Java:

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

Python:

class Solution:
    def bulbSwitch(self, n: int) -> int:
        return int(sqrt(n))

你可能感兴趣的:(LeetCode #319 Bulb Switcher 灯泡开关)