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.

二. 题目分析

题目大意是,有n只初始处于关闭状态的灯泡。你首先打开所有的灯泡(第一轮)。然后,熄灭所有序号为2的倍数的灯泡。第三轮,切换所有序号为3的倍数的灯泡(开着的就关掉,关着的就打开)。第n轮,你只切换最后一只灯泡。计算n轮之后还有几盏灯泡亮着。

先看看以下规律:

第1个灯泡:1的倍数,会改变1次状态: off -> on

第2个灯泡:1的倍数,2的倍数,会改变2次状态: off -> on -> off

第3个灯泡:1的倍数,3的倍数,会改变2次状态: off -> on -> off

第4个灯泡:1的倍数,2的倍数,4的倍数,会改变3次状态: off -> on -> off -> on

第5个灯泡:1的倍数、5的倍数,会改变2次状态: off -> on -> off

第6个灯泡:1的倍数,2的倍数,3的倍数,6的倍数,会改变4次状态: off -> on -> off -> on -> off

第7个灯泡:1的倍数、7的倍数,会改变2次状态: off -> on -> off

第8个灯泡:1的倍数,2的倍数,4的倍数,8的倍数,会改变4次状态: off -> on -> off -> on -> off

第9个灯泡:1的倍数,2的倍数,4的倍数,会改变3次状态: off -> on -> off -> on

……

通过上面的描述可以发现,对于n = 2,3,5,6,7,8,找到一个数的整数倍,总会找到对称的一个整数倍,例如 1 * 2,就肯定会有一个 2 * 1。因此这些编号的灯泡会改变偶数次,最终结果肯定为off

只有当n = 1,4,9这类完全平方数,有奇数次变化,最终的灯泡都会 on

只要能得出以上结论,用一句代码就可以解决问题。

三. 示例代码

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

四. 小结

对于一些组合类的算法题,找规律比直接动手实现更为重要。

时间: 2024-11-03 22:04:46

leetcode笔记:Bulb Switcher的相关文章

[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

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笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

leetcode笔记: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] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

【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