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 个灯 ...
随机推荐
- Asp.Net原理Version1.0
Asp.Net原理Version2.0 Asp.Net原理Version3.0_页面声明周期
- git撤销删除
问题描述: 使用git时本地文件删除了,提交至github,希望撤销修改,找回源文件 问题解决: (1)查看git log,查看日志信息 注: 使用 git log 可以查看提交的日志 ...
- [转载]淘宝API调用 申请 获取session key
http://www.cnblogs.com/zknu/archive/2013/06/14/3135527.html 在调用淘宝的API时,我们都会用到appkey,appsecret,appses ...
- js刷新
jquery刷新页面的实现代码(局部及全页面刷新) 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 jquery刷新页面的实现代码(局部及全页面刷新) ,需要的朋友可以参考下. 局部刷新: ...
- C#委托及事件
转载:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html C#委托及事件 在C#中,委托(delegate)是一种引用类型 ...
- Java中转UTC时间字符串(含有T Z)为local时间
在Java中我们需要转换相应格式的字符串,很多时候我们想到用SimpleDateFormat类来解析.但是最近我在调用一个第三方的接口时返回的 JSON字符串中有个expires字段的值是2014-0 ...
- sublime 复制黏贴等快捷键修改
在 keyboard-binding user 里 增加个人配置来覆盖默认配置 [ { "keys": ["ctrl+z"], "command&qu ...
- 李洪强iOS开发之上传照片时英文改中文
今天在做项目的时候,有一个功能是上传照片选择系统相册的照片,或者拍照上传照片,但是页面上的文字是英文的, 需求想改成中文的,解决方法是在 info.plist里面添加 Localized resour ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- [topcoder]PackingBallsDiv2
http://community.topcoder.com/stat?c=problem_statement&pm=12995 简单题 class PackingBallsDiv2 { pub ...