leetcode Bulb Switcher

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

题目地址:leetcode Bulb Switcher

题意:

给定n,初始的时候灯都是灭的,让你进行进行开关灯。第一轮的时候把1的倍数反转(原来关就开,原来开就关),第二轮把2的倍数翻转,以此类推到第n轮。

求最后灯打开的个数

思路:

我们来分析下:对于素数,那么它仅有1和它本身,最后一定是关掉的。

对一普通的,一定是关掉的,因子成对出现

对于完全平方数,因为有一个倍数不成对出现,所以一定是打开的。比如4 => 1,4   | 2

所以本题就是求1~n有几个完全平方数。

那么,怎么求呢?从1开始到n,每个数测试一下?时间复杂度O(n),太慢

正确的是直接sqrt(n),就可以算出来啦~

C++

Python

Java

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Bulb Switcher
本文地址:https://www.hrwhisper.me/leetcode-bulb-switcher/

您的支持将鼓励我继续创作!

Leetcode , , , . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *