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个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
随机推荐
- [NOIp2012提高组]同余方程
OJ题号: 洛谷1082 思路: 逆元模板. #include<cstdio> #include<cctype> inline int getint() { char ch; ...
- git恢复某个已修改的文件--备忘
checkout 恢复某个已修改的文件(撤销未提交的修改): $ Git checkout file-name revert 还原已提交的修改(已经提交过的修改,可以反悔-) 还原最近一次提交的修改: ...
- JS 显示周 几和 月 日
function getMyDay(date){ var week; ; var day = date.getDate(); ) week="周日" ) week="周一 ...
- Qt打包成单独可执行的exe文件
1.将图标newIco.ico复制到工程目录下. 2.在工程目录下新建空白txt文档,添加以下内容. IDI_ICON1 ICON DISCARDABLE "newIco.ico" ...
- oracle多个结果集拼接字符串;where id in 字符串 (转)
转自:http://blog.sina.com.cn/s/blog_af26e333010194ht.html 最近修改oracle触发器,在过程中遇到两个问题: select lastname fr ...
- oracle中的decode的使用(转)
地址:http://www.cnblogs.com/juddhu/archive/2012/03/07/2383101.html 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值 ...
- 机器学习笔记(4):多类逻辑回归-使用gluton
接上一篇机器学习笔记(3):多类逻辑回归继续,这次改用gluton来实现关键处理,原文见这里 ,代码如下: import matplotlib.pyplot as plt import mxnet a ...
- 异常 try catch finally return 执行关系 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- oracle 常用 sql
判断字段值是否为空( mysql 为 ifnull(,)): nvl (Withinfocode,'') as *** 两字段拼接: (1)concat(t.indate, t.intime) as ...
- SpringMVC项目配置欢迎页面为index.html
一.问题 在web.xml中添加如下配置无效 <welcome-file-list> <welcome-file>index.html</welcome-file> ...