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 ...
随机推荐
- 【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑
题意 给出一张无向图,求出恰巧经过n条边的最短路. 题解 考虑先离散化,那么点的个数只会有202个最多.于是复杂度里面就可以有一个\(n^3\).考虑构造矩阵\(d^1\)表示经过一条边的最短路,那么 ...
- P2055 [ZJOI2009]假期的宿舍
思路 看到复杂的匹配条件,发现要让一个人和一个床匹配,所以就每个有床的人(指本校学生)和t连一条边,每个需要床的人(指外校的人和不回家的人)和s连一条边,i和j互相认识就把i和j的床连在一起,自己和自 ...
- Shiro学习笔记(二)
首先还是先搭建工程运行环境 依旧搭建的是Maven工程,如果不是Maven 也可以去网上找jar包然后导入 (我使用Maven主要是找依赖配置文件就行,我自己导jar包的时候就是很容易报错) 还是先 ...
- NLog——ElasticSearch——Kibana
Nlog.elasticsearch.Kibana以及logstash在项目中的应用(一) Nlog.elasticsearch.Kibana以及logstash在项目中的应用(二) ASP.NET ...
- Kubernetes之Controllers一
ReplicaSet is the next-generation Replication Controller. The only difference between a ReplicaSet a ...
- JavaScript——语法与数据类型
严格模式 ECMA5引入了严格模式的概念.严格模式是为JavaScript定义了一种不同的解析与执行模型.在严格模式下,ECMA3中的一些不确定的行为将得到处理,而且对某些不安全的操作也会抛出错误.要 ...
- 2、My Scripts
http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html 注释详解 1.打印选择菜单,按照选择项一键安装不同的web服 ...
- CSDN-markdown编辑器语法——字体、字号与颜色
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过类似HTML的标记语法,它可以使普通文本内容具有一定的格式.但是它本身是不支持修改字体.字号与颜色等功能的! CSDN-markd ...
- GZip对字符串压缩和解压
/// <summary> /// 压缩 /// </summary> /// <param name="value">需要压缩字符串</ ...
- ThreadPoolExecutor最佳实践--如何选择线程数
去年看过一篇<ThreadPoolExecutor详解>大致讲了ThreadPoolExecutor内部的代码实现. 总结一下,主要有以下四点: 当有任务提交的时候,会创建核心线程去执行任 ...