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 toggle the last bulb. Find how many bulbs are on after n rounds.
问题分析:
/**
*
* 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.
*
* 一、从给定的题目中找到规律:就是,当 i 为 1 到 n之间的任何数时,翻转 k = ri 位置上灯泡的状态。
* 求n轮之后,还有几盏灯是开着的?
* 二、最直观的方法是穷举法:一轮轮的遍历,但是,当n很大时,时间复杂度太大。
* 三、找规律:
* 若起初有5盏灯,经过5轮翻转后,最终只有第1盏和第4盏是亮着的,不妨分析下,这里面是否有什么规律?
* 起始:5盏灯全部为 off
* 1 = (1,1) 1次翻转,最终on
* 2 = (1,2),(2,1) 2次翻转,最终off
* 3 = (1,3),(3,1) 2次翻转,最终off
* 4 = (1,4),(2,2),(4,1) 3次翻转,最终on
* 5 = (1,5),(5,1) 2次翻转,最终off
*
* 可以看出,最终为on的灯所在位置都是平方数
* @author admin
*
*/
代码:1到n之间 平方数的个数 为: sqrt(n) 向下取整。
//返回n轮之后,有多少盏灯是开着的
public static int bulbSwitch(int n){
if(n <= 0)
return 0;
int num = (int)Math.sqrt(n); //求n的平方根,double强制转换为int类型,即为向下取整。 System.out.println(num); return num;
}
Bulb Switcher (leetcode java)的更多相关文章
- [LeetCode] Bulb Switcher 灯泡开关
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LC 672. Bulb Switcher II
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- LeetCode Bulb Switcher 319
变换灯泡颜色 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- Leetcode Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
随机推荐
- [SDOI2016]游戏 树剖+李超树
目录 链接 思路 update 代码 链接 https://www.luogu.org/problemnew/show/P4069 思路 树剖+超哥线段树 我已经自毙了,自闭了!!!! update ...
- 《操作系统_时间片轮转RR进程调度算法》
转自:https://blog.csdn.net/houchaoqun_xmu/article/details/55540250 时间片轮转RR进程调度算法 一.概念介绍和案例解析时间片轮转法 - 基 ...
- Bytom交易说明(账户管理模式)
比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 该部分主 ...
- Shell: nohup守护进程化
如果想在终端会话中启动shell脚本,然后让脚本一直以后台模式运行,直到其完成,即使你退出了终端会话,可以使用nohup命令实现.感觉nohup就是将一个进程初始化为一个守护进程. nohup命令运行 ...
- 如何某个js文件中的 console
因为自己引用了别人的一个 js 文件,但里面有很多事件相关的 console 输出.自己并不想去修改别人的文件.但想屏蔽掉里面的 console . 有多个 js 文件里有 console.log . ...
- SAP本地化-银企直连
SAP本地化-银企直连 http://blog.sina.com.cn/s/blog_a0de8cc80101dee1.html 一.发展历史 2011年,在SAP ECC6 Ehp5中,通过功能增强 ...
- java中垃圾回收算法讲解
判断对象是否存活的方法: 一.引用计数算法(Reference Counting) 介绍:给对象添加一个引用计数器,每当一个地方引用它时,数据器加1:当引用失效时,计数器减1:计数器为0的即可被回 ...
- 同上篇 这篇是针对mesh的
资源加载性能测试代码 与上篇所提出的测试代码一样,我们对于其他资源的加载性能分析同样使用该测试代码.我们将每种资源均制作成一定大小的AssetBundle文件,并逐一通过以下代码在不同设备上进行加载, ...
- 数据从mysql迁移至oracle时知识点记录(一)
最近在做数据的迁移,再将数据从mysql迁移至oracle时,部分sql语句进行了修改,在此对部分知识点进行记录: 参考资料:https://dev.mysql.com/doc/refman/5.5/ ...
- Linux系统vi或者vim编辑器中如何显示行号
设置行号很简单 我们要到vi或者vim编辑器的命令模式下,输入set number :set number 按下回车就显示行号了 那么怎么关闭行号呢? 很简单,我们只要再到vi或者vim编辑器的命令模 ...