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
public static int bulbSwitch(int n){
return (int)Math.sqrt(n);
}
LeetCode Bulb Switcher 319的更多相关文章
- [LeetCode] Bulb Switcher 灯泡开关
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- Leetcode Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- 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 ...
- 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 ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
随机推荐
- 【原】灵活运用sessionStorage或者localStorage
有时,一个app中,后台并没有提供页面中对应的信息接口,需要前端在页面跳转时把某些信息带入下一个页面,一般想到用url后带参数的方法,但是有时需要带的参数过长,就不适合用这个方法了,所以用sessio ...
- 流水灯 外侧<->中间<->外侧
/* //side<-center->side //side->center<-side //loop *// #include "reg52.h" voi ...
- Python torndoa mysql 模块安装
pip install torndb pip install pip install mysql-python #不支持3.x版本 ln -s /usr/local/mysql/lib/libmysq ...
- mysql重复记录的查询删除方法
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from ...
- [DFNews] EnCase 更新至 v7.10
有加密狗的可以注册接收邮件下载 暂时只有英文版 前几天讲课还说到,EnCase的Template倒是好,但是稍微改一下Case Template自带的Bookmark结构,那么Report就看不到了, ...
- JQuery 在循环中设置事件,最后一个覆盖了前面所有的设置
function setValidation() { for (i = 0; i < alValidations.length; i++) { //alValidations是一 ...
- 第一个Leap Motion测试页面 (webgl/three/leapjs/leap)
div#canvas-frame{ border: none; cursor: pointer; width: 100%; height: 800px; background-color: #EEEE ...
- vs2010连接postgresql数据库
Windows环境C/C++访问PostgreSQL主要有两种方式:利用Qt封装的数据库访问组件.利用PostgreSQL的API函数.使用Qt平台访问PostgreSQL的局限性很大,一旦脱离了访问 ...
- 利用IDL将一个txt文档拆分为多个
测试.txt文档,每47行的格式相同,通过代码每47行存为一个txt,txt文档命名为其第一行数据. 代码如下: file='G:\data\测试.txt' openr,lun,file,/Get_L ...
- GNS3 桥接虚拟网卡 telnet 实验
网上很多桥接本地网卡的,一直测试不通.无奈,本人桥接vmware 虚拟网卡通! 1: 2: 3:telnet 加密实验 R1(config)#line vt R1(config)#line vty 0 ...