Leetcode 319 Bulb Switcher 找规律

有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭。

class Solution {
public:
    int bulbSwitch(int n) {
        return (int)sqrt(n*1.0);
    }
};
时间: 2024-10-19 03:21:43

Leetcode 319 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][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

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

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

319 Bulb Switcher 灯泡开关

初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯泡切换一次开关. 对于第 n 轮,你只切换最后一个灯泡的开关. 找出 n 轮后有多少个亮着的灯泡.示例:给定 n = 3.状态off表示灯泡关闭,on表示开启.初始时, 灯泡状态 [off, off, off].第一轮后, 灯泡状态 [on, on, on].第二轮后, 灯泡状态 [on, off,

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

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 every i

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4