变换灯泡颜色

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

按题意coding

public int bulbSwitch(int n){
int[] arrN = new int[n];
int count = 0;
for (int i = 0; i < arrN.length; i++) {
arrN[i] =0;
}
for (int i = 0; i < arrN.length; i++) {
for (int j = 0; j < arrN.length;j++ ) {
if(i==0){
arrN[j] = 1;
// j++;
}else{
int k = i+1;
if((j+1)%k==0){arrN[j]=(arrN[j]==1?0:1);}
// j=j+i;
}
}
} for (int i = 0; i < arrN.length; i++) {
if (arrN[i]==1) {
count++;
}
}
return count;
}

运行超时:

灯泡颜色的变换只存在于自己的因子处

1.素数,只有1和自己。

2.普通数,因子成对出现。

3.只有完全平方数灯泡奇数次点亮不会熄灭。

此题转化为求完全平方数的个数!!!

比如:3-->1,4-->2,10-->3

 public static int bulbSwitch(int n){
return (int)Math.sqrt(n);
}

LeetCode Bulb Switcher 319的更多相关文章

  1. [LeetCode] Bulb Switcher 灯泡开关

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

  2. [LeetCode] Bulb Switcher II 灯泡开关之二

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

  3. Leetcode Bulb Switcher

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

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

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

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

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

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

  7. Java [Leetcode 319]Bulb Switcher

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

  8. LeetCode 319. Bulb Switcher

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

  9. Leetcode 319 Bulb Switcher 找规律

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

随机推荐

  1. 小例子 熟悉jquery

    <div class="tab-head"> <h2 id="tab1" class="selected">JQGr ...

  2. three.js学习笔记

    一.名词解释 scene - 场景 camera - 摄影机 renderer - 渲染器:描绘器 Vector - 三维向量的对象 orthographic - 正射 field of view - ...

  3. Button--防止button多次点击

    ================================= //代码2 public abstract class NoDoubleClickListener implements OnCli ...

  4. Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化

    Tomcat Connector的三种不同的运行模式性能相差很大,有人测试过的结果如下: 这三种模式的不同之处如下: BIO: 一个线程处理一个请求.缺点:并发量高时,线程数较多,浪费资源. Tomc ...

  5. LINUX 磁盘如何分区

    fdisk -l  可以查看当前磁盘 假设未分配磁盘为/dev/sdb    size=10G fdisk /dev/sdb  (m for help) 按照提示应该可以分区成功,注意一点   一个磁 ...

  6. 【HOW】如何允许编辑用户配置文件属性

    在自定义用户配置文件属性后会发现,通过属性本身的配置页面,只能允许用户修改自己的属性,而管理员无法修改其他用户的属性.若要允许管理员在用户信息页面修改用户配置文件属性,可通过如下操作实现: 1. 进入 ...

  7. PHP表单数据验证

    背景: 在上次项目的时候,一直不明白为什么要对数据验证,我能保证我每次请求的数据都是合法的,但是在后面的时候,原来“用户”并不是那样听话,他总是要给我们找麻烦,然后可能让我们的服务器崩掉.但是只对单个 ...

  8. .net framework 3.5sp1 安装不成功

    主要还是进程中有程序和正在安装的 framework 3.5sp1有冲突.打开任务管理器,关闭所有360进程和与Framework有关的进程.竟然安装成功了,折腾了3.4天的问题,就这么解决了.

  9. 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

    迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...

  10. js实现图片无缝连接

    效果图 1.首先先看看html和css代码 <style> *{padding:0;margin:0;} #div1{margin:100px auto;background:red;wi ...