星空网 > 软件开发 > Java

LeetCode Bulb Switcher 319

变换灯泡颜色

 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

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

 




原标题:LeetCode Bulb Switcher 319

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

fba头程厦门:https://www.goluckyvip.com/tag/20032.html
fba头程上海:https://www.goluckyvip.com/tag/20033.html
fba头程深圳:https://www.goluckyvip.com/tag/20034.html
fba头程什么意思:https://www.goluckyvip.com/tag/20035.html
fba头程时效:https://www.goluckyvip.com/tag/20036.html
fba头程是什么:https://www.goluckyvip.com/tag/20037.html
37号文今后是否会更新?一文详解关键信息 :https://www.kjdsnews.com/a/1836441.html
探讨内地人开设香港账户的可行性 :https://www.kjdsnews.com/a/1836442.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流