[LeetCode][JavaScript]Bulb Switcher

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.

https://leetcode.com/problems/bulb-switcher/



题目意思是一开始灯泡都是关的,从2开始的第n轮就开关n倍数的灯泡,打开原来关的灯泡,关上开着的。

一看就是数学题,搞个数组去模拟肯定TLE,有大数,需要找规律。

像我这种数学拙计的选手,就是死算,从1推到10应该就能看出规律了,所有剩下的灯泡都是平方数1, 4, 9。

看看大神的解释:

https://leetcode.com/discuss/75014/math-solution

我的理解是,比如12可以拆成1和12, 2和6, 3和4,他们两两之间碰不到,或者碰到奇数次,最后是关的。

而像10, 11这种只可能碰到一次,最后是关的。

如果是平方数比如9,3的时候关了,9的时候又会打开,所以最后是打开的。

1 /**
2  * @param {number} n
3  * @return {number}
4  */
5 var bulbSwitch = function(n) {
6     return parseInt(Math.sqrt(n));
7 };
时间: 2024-10-27 03:49:56

[LeetCode][JavaScript]Bulb Switcher的相关文章

Leetcode 319. Bulb Switcher

这是一个非常巧妙的题目,网站称之为brain teaser,意思就是代码写起来会非常简介(就是逗你玩:)),题目如下: 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 tu

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 ro

Leetcode 319 Bulb Switcher 找规律

有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt(n*1.0); } };

[LeetCode][JavaScript]Pascal's Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

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

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 th

Bulb Switcher (leetcode java)

问题描述: 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 tog

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 t

319. 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 ith round, you toggle eve