变换灯泡颜色

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. 几种加解密方法:AES、DES、SHA数据加密

    一般项目都会用上加密,刚好手上的项目就用到DES加密,就贴一些代码记录一下 DES加密步奏: 1.初始化两个字符串,一个为指定的秘钥,一个为初始化向量,要求是8个字符. 2.加密:秘钥.向量.需加密的 ...

  2. CE程序

    注:开发工具用VS2008 安装Windows mobile设备中心进行调试. 项目平台 1.窗体属性设置,然后将size改成238, 320 2.效果 3.调试过程选择“部署” 4.配置文件的简单读 ...

  3. STM32串口

    状态寄存器USART_SR的复位值为0x00C0H:即 第七位发送寄存器空TXE=1和第六位发送完成TC=1寄存器为空, TC=1表明发送已完成.此时只要使能了发送空和发送完成中断就可进入中断ISR( ...

  4. Sql Server分割字符串函数

    -- Description: 分割字符串函数 -- SELECT * FROM dbo.Split('a,b,c,d,e,f,g',',') -- ========================= ...

  5. linux socket编程实例

    /* ============================================================================ Name : client.c Auth ...

  6. 查看regulator的信息

    例如,查看ldo6 /sys/devices/soc.0/qcom,rpm-smd.35/rpm-regulator-ldoa6.85/regulator-l6.110/regulator/regul ...

  7. yum命令——安装、卸载、查询等

    --常用命令 1.安装软件 yum install 软件名称 2.卸载软件 yum remove 软件名称 3.更新软件 yum update 软件名称 4.列出所有可安装的软件包 yum list ...

  8. 剑指Offer:面试题30——最小的k个数(java实现)

    问题描述: 输入n个整数,找出其中最小的k个数 思路1: 先排序,再取前k个 时间复杂度O(nlogn) 下面给出快排序的代码(基于下面Partition函数的方法) public void Quic ...

  9. Asp.net core 通过Models 生成数据库的方法

        其实Getting Started当中有着详细的说明,https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html ...

  10. 嵌入式: jffs2,yaffs2,logfs,ubifs文件系统性能分析

    在嵌入式领域,FLASH是一种常用的存储介质,由于其特殊的硬件结构,所以普通的文件系统如ext2,ext3等都不适合在其上使用,于是就出现了专门针对FLASH的文件系统,比较常用的有jffs2,yaf ...