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的更多相关文章

  1. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  2. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  3. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  4. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  5. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  6. 319. Bulb Switcher

    题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...

  7. 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 ...

  8. 319 Bulb Switcher 灯泡开关

    初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯 ...

  9. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

随机推荐

  1. S1 : 传递参数

    ECMAScript 中所有函数的参数都是按值传递的.也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样.基本类型值的传递如同基本类型变量的复制一样,而引用类型值的传 ...

  2. 常用三方,Reachability 检测网络连接

    常用三方 Reachability 检 测网络连接 用来检查网络连接是否可用:包括WIFI和 WWAN(3G/EDGE/CDMA等)两种工作模式. 可以从Apple网站下载到: http://deve ...

  3. PHPExcel 学习笔记

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...

  4. vsftp 配置

    安装和基本配置网上很多文章,但他们的最终效果不是我想要的: 我想要的是,ftp上传的文件用户可以通过apache的http服务访问,也就是ftp上传的文件可以通过浏览器访问,并且可以通过ftp客户端修 ...

  5. c#中使用servicestackredis操作redis

    下载地址: https://github.com/mythz/ServiceStack.Redis 添加dll引用: using ServiceStack.Common.Extensions;usin ...

  6. 创建ROS功能包(四)

    为了方便直接用ROS的create-pkg命令行工具 roscreate-pkg chapter2_tutorials std_msgs rospy roscpp std_msgs 包含了常见的消息类 ...

  7. python处理url中的中文编码,以及其他编码问题

    1.python中的urlencode与urldecode 2.各种编码转换在线工具 3.python用于url解码和中文解析的小脚本(python url decoder) 4.如何只对url中的中 ...

  8. [开发笔记]-获取歌曲ID3信息

    ID3介绍: ID3,一般是位于一个mp3文件的开头或末尾的若干字节内,附加了关于该mp3的歌手,标题,专辑名称,年代,风格等信息,该信息就被称为ID3信息,ID3信息分为两个版本,v1和v2版. 获 ...

  9. 支持.NET和移动设备的XLS读写控件XLSReadWriteII下载地址及介绍

    原文来自龙博方案网http://www.fanganwang.com/product/3085转载请注明出处 读写任何单元值 数字型.字符型.布尔型以及错误型.但是你了解日期和时间型单元吗?在Exce ...

  10. iOS开发之单例设计模式(完整正确版本)

    单例的意思从字面上就可以略知一二,所谓单例就是确保在程序运行过程中只创建一个对象实例.可以用于需要被多次广泛或者说多次使用的资源中,比如我们常见的网络请求类.工具类以及其它管理类等.比如我iOS开发中 ...