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 every i bulb. 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.
链接: https://leetcode.com/problems/bulb-switcher/
题解:
类似智力题。划一下5个灯泡的流程图就会发现最后亮的是1号和4号灯泡。最后结果就是求在n里包含多少个完全平方数。 在Discuss里Stefan Pochmann写得很好,放在reference里。
Time Complexity - O(1), Space Complexity - O(1)。
public class Solution {
public int bulbSwitch(int n) {
if (n < 1) {
return 0;
}
return (int)Math.sqrt(n);
}
}
二刷:
Java:
public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}
Reference:
https://leetcode.com/discuss/75014/math-solution
319. Bulb Switcher的更多相关文章
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- 319. Bulb Switcher——本质:迭代观察,然后找规律
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 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 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 319. Bulb Switcher (Math, Pattern)
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 319 Bulb Switcher 灯泡开关
初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯 ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
随机推荐
- 11,EasyNetQ-调度事件与定时发布
许多业务流程都要求将事件安排在未来的某个日期. 例如,在与客户进行初次销售联系之后,我们可能希望在将来某个时间安排后续电话. EasyNetQ可以通过其未来发布功能帮助您实现此功能. 例如,我们在这里 ...
- 51Nod.1244.莫比乌斯函数之和(杜教筛)
题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...
- 并查集 (Union-Find Sets)及其应用
定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的 ...
- j2me必备之网络开发数据处理
第9章 无线网络开发MIDP提供了一组通用的网络开发接口,用来针对不同的无线网络应用可以采取不同的开发接口.基于CLDC的网络支持是由统一网络连接框架(Generic Connection Frame ...
- WCF、WebAPI、WCFREST和Web服务的差异 ASP.NETMVC和ASP.NETWebAPI的差异
WCF.WebAPI.WCFREST和Web服务的差异: Web服务 它是基于SOAP和XML的形式返回数据. 它仅支持HTTP协议. 它是开放源,但是不消耗任何客户端可以同时理解XML. 它可以仅在 ...
- Structured Streaming教程(1) —— 基本概念与使用
近年来,大数据的计算引擎越来越受到关注,spark作为最受欢迎的大数据计算框架,也在不断的学习和完善中.在Spark2.x中,新开放了一个基于DataFrame的无下限的流式处理组件--Structu ...
- JS版日期格式化和解析工具类,毫秒级
/** * ===================================== * 日期相关方法 * ===================================== */ ;(fu ...
- Nginx升级
Ubuntu14.04默认的安装源中安装的是Nginx 1.4.6 echo deb http://nginx.org/packages/ubuntu/ trusty nginx >> / ...
- mod_wsgi的工作模式和配置
Openstack所有提供API接口的服务都是python web server,而其本身性能很弱,目前已经将它们配置到了apache上.但对于如何设置mod_wsgi的参数,我一直没有好好去阅读其文 ...
- awstats日志分析
nginx日志格式设定,去掉如下行#号 #vim /usr/local/nginx/conf/nginx.conf log_format access '$remote_addr -$remote_u ...