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 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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
Example:
Input: 3
Output: 1
Explanation:
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.
Solution: find the pattern among them:
n : res
(1--3): 1
(4--8): 2
(9--15): 3
(16--24): 4
basically, sqrt(n).toint()
class Solution {
public int bulbSwitch(int n) {
if(n==0) return 0;
for(int i = 1; i<=Math.sqrt(n); i++){
if(n>=i*i && n<=(i+2)*i){
return i;
}
}
return 0;
}
}
319. Bulb Switcher (Math, Pattern)的更多相关文章
- 【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 ...
- 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
题目: 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 ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- 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( ...
随机推荐
- Android 调整图标和字体大小
1. Root 2. 进system,找到build.prop 3. 用RE管理器,编辑 ro.sf.lcd_density=320, 后面的数值随意调整,越大图标越大,不要太贪心,图标变大会显示不全 ...
- scrapy模块之分页处理,post请求,cookies处理,请求传参
一.scrapy分页处理 1.分页处理 如上篇博客,初步使用了scrapy框架了,但是只能爬取一页,或者手动的把要爬取的网址手动添加到start_url中,太麻烦接下来介绍该如何去处理分页,手动发起分 ...
- PIE SDK聚类
1.算法功能简介 聚类处理时运用形态学算子将临近的类似分类区域聚类并合并. PIE SDK支持算法功能的执行,下面对聚类算法功能进行介绍. 2.算法功能实现说明 2.1. 实现步骤 第一步 算法参数设 ...
- 2.5 References & Borrowing
Here is how you would define and use a calculate_length function that has a reference to an object a ...
- 关于箭头函数的this指向问题
document.onclick = function(){ // 普通函数的this是在运行的时候才临时绑定的,也就是说,函数不运行,你绝对不可能知道this是谁 // 下面这个函数如果是自调用,t ...
- gulp打包js
在终端定位到你要创建目录的地方,输入 sudo mkdir js 创建文件夹,这个文件夹就是放你要压缩js文件的地方 输入 sudo vim gulpfile.js 这个js就是写gulp所有的配置信 ...
- 如何添加网页QQ在线组件
免费开通QQ推广服务 复制代码到你想要展示的位置 粘贴,完成.
- Git~分支真的很轻
轻,让人觉得很爽 所有源代码管理工具都有管理分支的功能,git当然也不例外,而且git的分支是非常轻的,不像tfs,svn那样,复制一大堆代码,git只记录变化的内容,有本地分支与远程分支之分,原则上 ...
- 移动端或APP禁止放大标识
如果手机端或者APP的应用里面,有点击一下屏幕会自己放大,解决办法如下: 在头部添加一条meta标识 <meta name="viewport" content=" ...
- 译:面试投行的20个Java问题
原文链接:https://dzone.com/articles/var-work-in-progress 作者:Anghel Leonard 译者:沈歌 如果你需要准备面试,可以看一下这篇博客中20个 ...