LeetCode Bulb Switcher

news/2025/5/24 9:23:41

题目:

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盏灯,初始化的时候是处于turn off(关闭)的状态,第一次的时候是将所有的灯全部点亮,第二轮的时候,是将所有的2的倍数的盏灯关闭,以此类推。。。问到第n轮之后,最后剩下多少盏灯还亮着。

题解:

此题是放在数学里的,而且也明确表明是brainteaser(脑筋急转弯)。那么必然有很巧妙的方法。所以在做的时候,我们考虑比较特殊的方法。首先通过仔细观察会发现,因为一直是关于倍数的,比如3,那么它是1->3,所以它在灭灯的时候是1,先开启灯,然后到3的时候灭灯,而之后的状态都和它无关,所以之后都关灯的,如果是4,那么它是1->2->4,其实是1 * 4 -> 2 * 2 -> 4 * 1,所以它是先开灯,然后关灯,接着再开灯,之后的状态与它也无关,所以最后它的状态就是开着的。所以发现了规律,如何判断这盏灯是否是关或者是开,采用的是如果这个盏灯的数字是平方根,那么它最后必定是开的,所以只需要判断这个n之前出现的最大的那个平方根数就行了。

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

代码非常简单,但是仔细分析就是关键。

文章来源:https://blog.csdn.net/sun_wangdong/article/details/50448196
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-2654227.html

相关文章

C++操作MySQL,有用的朋友顶下,辛苦的原创啊. - 天下 - C++博客

C操作MySQL,有用的朋友顶下,辛苦的原创啊. - 天下 - C博客C操作MySQL,有用的朋友顶下,辛苦的原创啊.向google大神搜 :mysql-connector得http://www.mysql.com/products/connector/这些就是mysql所谓的连接器吧.一路向下看到:C Wrapper for MySQL C API (MySQL) Download http:/…

sklearn学习——递归特征消除法(RFE)

sklearn学习——递归特征消除法(RFE) 1 作用 消除特征之间的冗余,选取最优特征组合。降低特征维数。 2 步骤 将筛选的k个特征作为初始特征子集输入到随机森林分类器中,计算得到每个特征的重要性,并利用交叉验证方法…

Java Code Review清单

2019独角兽企业重金招聘Python工程师标准>>> 整洁的代码 清单项目分类使用可以表达实际意图(Intention-Revealing)的名称有意义的名称每一个概念只用一个词有意义的名称使用方案/问题领域名称有意义的名称类应该是比较小的!类函数应该是比较小的!函数只做一件事函数…

LeetCode Generate Parentheses

题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()(…

python起步——可变对象和不可变对象

学习python了一小段时间,觉得整体上还是真的让程序更好写了。   学习过程中,突然想到一个问题——我之前博客写过的一篇文章,关于不用第三个数交换a、b的问题:http://www.cnblogs.com/FreeAquar/archive/2012/07/22/2603381.htm…

Java学习笔记(1)——常用cmd命令与Java编制编译

Java学习笔记——常用cmd命令与Java编制编译 1 JDK下载安装与环境配置 附上链接 2 常用cmd命令 dir 列出当前目录下的文件以及文件夹md 创建目录rd 删除目录cd 进入指定目录cd… 退回到上一级目录del 删除文件 3 Java特点 简单性面向对象分布式健壮性安全性体系结构中立…

华为防火墙-适合CSSIP方向

新版的OS初始console的用户名:admin,密码:Admin123连接console进入设备: Copyright(C) 2010-2013 Huawei Technologies Co., Ltd. *All rights reserved *Without the owners prior written consent, *no decompiling or reverse-…

LeetCode Letter Combinations of a Phone Number

题目: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", &q…

Java学习笔记(2)——基础语法

Java学习——基础语法 1 第一个Java程序 public class 后面采用的类名和文件名保持一样,一个Java程序里面只有一个public class;class后面类名必须以字母开头,后面可以跟字母和数字的任意组合;System.out.println(&a…

LeetCode Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJs undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and …