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.
分析:http://www.programcreek.com/2014/05/leetcode-bulb-switcher-java/
By using some examples we can find out the number of switches for each bulb:
1 -> 1 (1)
2 -> 2 (1 2)
3 -> 2 (1 3)
4 -> 3 (1 2 4)
5 -> 2 (1 5)
6 -> 4 (1 2 3 6)
7 -> 2 (1 7)
8 -> 4 (1 2 4 8)
9 -> 3 (1 3 9)
So only (i*i)th element has odd number of switches and they are on. The problem is now get all the square numbers.
public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}
最直接的方法:
public class Solution {
public int bulbSwitch(int n) {
int count = ;
for (int i = ; i <= n; i++) {
int numSwitch = helper(i);
if (numSwitch % == )
count++;
} return count;
} public int helper(int kthBulb) {
int count = ;
for (int i = ; i <= kthBulb; i++) {
if (kthBulb % i == )
numSwitch++;
}
return numSwitch;
}
}
Bulb Switcher的更多相关文章
- [LeetCode] Bulb Switcher 灯泡开关
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- LC 672. Bulb Switcher II
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- LeetCode Bulb Switcher 319
变换灯泡颜色 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- Leetcode 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 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 ...
随机推荐
- WEB中的cookie
首先来一篇好文章,刚好看到的: 沉默中的狂怒 —— Cookie 大喷发---------------- http://www.cnblogs.com/index-html/p/mitm-cookie ...
- 坑爹的BFC;块格式上下文
Formatting context(FC) Formatting context 是 W3C CSS2.1 规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位 ...
- 整数划分 (区间DP)
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- hdu2807 矩阵乘法+floyd
网上有优化的方法 就是乘上一个一维的矩阵:现在还没有想通.想通了不上代码: 我用的就是普通的矩阵,压着时间过:只是多了一个判断条件,不加这个条件就超时: #include<stdio.h> ...
- 【poj1050】 To the Max
http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...
- [NOIP2010] 提高组 洛谷P1540 机器翻译
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- 车牌号对应归属地及城市JSON带简码
车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...
- MVC区域 视图必须派生自 WebViewPage 或 WebViewPage<TModel>
http://blog.csdn.net/iack_ji/article/details/16965885 今天在学习 mvc区域时,将区域控制器类 外迁到其他的程序集的练习中出现了"视图必 ...
- easyUI框架之学习3--表格datagrid
@model MVCEasyUI.Models.Sale.PageResult<MVCEasyUI.Models.Sale.Order> @{ ViewBag.Title = " ...
- JNI的某些数组和字符串类型转换
JNICC++C#Windows jbytearray转c++byte数组 jbyte * arrayBody = env->GetByteArrayElements(data,0); jsiz ...