Java [Leetcode 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.
解题思路:
这道题目主要是看每个位置是否是被变化了奇数次。比如对于6,可以写成1x6、2x3,那么经过整个n次变化,这个位置经历了4次变化,所以是灭的状态;而9可以写成1x9、3x3,那么其经过3次变化,所以是点亮的状态;顺着这个思路,能够开方为整数的数字都是点亮的状态,所以只需要对整个n开方,然后向下取整即可。
代码如下:
public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}
Java [Leetcode 319]Bulb Switcher的更多相关文章
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- LeetCode 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- 【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 ev ...
- 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 个灯 ...
随机推荐
- C#中Json和List/DataSet相互转换
#region List<T> 转 Json /// <summary> /// List<T> 转 Json /// & ...
- [转载]C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
//防止页面多次刷新页面执行 ) { string GetUserName = System.Configuration.ConfigurationSettings.AppSettings[" ...
- c++ 钻石继承
在C++中,什么叫做钻石问题(也可以叫菱形继承问题),怎么避免它? 下面的图表可以用来解释钻石问题. 假设我们有类B和类C,它们都继承了相同的类A.另外我们还有类D,类D通过多重继承机制继承了类B和类 ...
- VB程序破解之API断点[bp __vbaVarTstEq]
软件名称:风云足彩1.7软件大小:2.1M下载地址:http://free.ys168.com/?zhinengxuanhao软件保护:注册码编写软件:Microsoft Visual Basic 5 ...
- 从后端到页面:如何全方位监控 Ruby 应用?
[编者按]本文参考技术分享 ,由 OneAPM 工程师补充整理,并且已经征得原作者的同意. 为什么选择 OneAPM ? 在性能监控领域,业界比较有名的是 New Relic 还有 Appdynami ...
- Python-aiohttp百万并发
http://www.aikaiyuan.com/10935.html 本文将测试python aiohttp的极限,同时测试其性能表现,以分钟发起请求数作为指标.大家都知道,当应用到网络操作时,异步 ...
- uniqueidentifier 数据类型(转)
想要产生这种唯一标识的格式的数据: 6F9619FF-8B86-D011-B42D-00C04FC964FF 应该怎么做呢?答: uniqueidentifier 数据类型可存储 16 字节的二进制 ...
- hdu 1879 继续畅通工程(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<algo ...
- 利用hadoop自带程序运行wordcount
1.启动hadoop守护进程 bin/start-all.sh 2.在hadoop的bin目录下建立一个input文件夹 JIAS-MacBook-Pro:hadoop- jia$ mkdir inp ...
- intellij idea 12、13 win8 下 中文输入覆盖的问题(搜狗输入法或者其他输入法)
最近升级到idea12,发现中文输入存在问题,输入中文的时候会出现空格,并且覆盖后面的字符,这个问题让我很郁闷. 假设idea的安装位置为:D:\Program Files\JetBrains\Int ...