319. Bulb Switche
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.
代码如下:(超时,但运行结果正确)
public class Solution {
public int bulbSwitch(int n) {
if(n==0)
return 0;
if(n==1)
return 1; int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=1;
for(int j=2;j<=n;j++)
{
int k=1;
while(j*k-1<=n-1)
{
if(a[j*k-1]==0)
a[j*k-1]=1;
else
a[j*k-1]=0;
k++;
}
}
int count=0;
for(int j=0;j<n;j++)
{
if(a[j]==1)
count++;
}
return count;
}
}
319. Bulb Switche的更多相关文章
- 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
题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...
- 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( ...
随机推荐
- java中的日志组件-log4j
1.为什么使用日志组件 Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT的事件记录器:还可以控制每一 ...
- 根据窗体自动调整控件及文本框记住上次填写内容Demo
第一次写文章,组词难免没有不通之处... 最近常用到Winform根据窗体大小自动调整空间大小及字体.文本框记住上次填写内容待下次输入某一段时候自动跳出上次输入内容.于是就随便把两个问题放到同一个de ...
- COJ 1287 求匹配串在模式串中出现的次数
这里要在后缀自动机的节点中维护一个从到达当前位置出现的字符串总个数 这里新添加进来的节点的状态出现的次数必然为1 另外包含所能达到这个节点所能到达的状态一定是将它作为父亲的点 那么说明将它作为父亲的点 ...
- Quartz2D
http://donbe.blog.163.com/blog/static/138048021201052093633776/ 详解 代码如下: DJView 绘制线段 基本图形 // // DJVi ...
- CodeForces 546A-Soldier and Bananas
题意: 有n dollar,the first banana cost k dollars,第i个就需cost k*i,问买w个bananas是否需要借钱:借钱需要多少? 分析:首先计算w个bana ...
- 关于Spatial referencing by geographical identifiers 标准
地理信息空间参考大体可以分为两类,ISO给出了分类:Spatial referencing by geographical identifiers(根据地理标识符的空间定位,ISO 19112)与Sp ...
- Visual Studio安装过程
在这里需要先跟老师说一声抱歉,因为编写代码的愿意,我早在大一的时候就已经安装并且购买了正版的VS2013.所以今天在这里实在无法全部描述VS2013的安装过程. 然而,我所知的是,VS2013相对于我 ...
- 关于强制类型转换(c语言)
因为今天看的代码中用到了结构体的强制类型转换,就很想了解一下结构体的强制类型转换是怎样的. 一个结构体如下: 在下面这段代码中rbuf->reqCmdBuf是一个空指针,首先将这个空指针赋值给一 ...
- 计算文字的高度和宽度--以微博会话界面中用户名(userName)为例
所用方法 // NOTE: All of the following methods will default to drawing on a baseline, limiting drawing t ...
- SharePoint 2013 Nintex Workflow 工作流帮助(七)
博客地址 http://blog.csdn.net/foxdave 工作流动作 11. Check out item(Libraries and lists分组) 与上一个对应,用于签出条目.如果一个 ...