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( ...
随机推荐
- JAVA 取得当前目录的路径/Servlet/class/文件路径/web路径/url地址
在写java程序时不可避免要获取文件的路径...总结一下,遗漏的随时补上 1.可以在servlet的init方法里 String path = getServletContext().getRealP ...
- 使用Jenkins可持续集成maven项目
首先下载最新的Jenkins的war包,放在tomcat的webapps的目录下,然后运行,例如: http://121.42.62.45:8080/jenkins/ 然后按照一步步的提示,下载相关的 ...
- thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错
thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错,开启调试模式,不报错,怎么解决? 查看Logs日志记录: [ --29T09::+: ] 113.108.11 ...
- Ext 三级联动 及附值
/// <reference path="../../ext.js" />Ext.define('Myview.Region', { extend: 'Ext.form ...
- css中 input的button默认原始的样子
以往我们写css时,让一行文字垂直居中. 会设置line-height等于height比如: 当我把这个原理 放在button上时 会是这个样子的. 以下都是火狐浏览器环境 有没有发现一个现象,他们 ...
- Druid 基础使用-操作篇(Imply )
一.Imply Druid 原生的配置较麻烦,在上一篇单机版安装中有所涉及 Imply 基于Druid 进行了一些组件的开发,提供开源社区版本和商业版,简化了部署,开发了一些应用.https:// ...
- ipad2 恢复
1.用原装充电线连接电脑,并打开itunes~2.同时按住电源键和home键 10秒左右,直到白苹果画面变成黑屏3.按住home键~但要松开电源键,继续等待~直到ipad出现画面(如图) 4.这时候, ...
- Java面向对象㈠ -- 封装
Java的面向对象有三大特征:封装.继承.多态.这里主要对封装进行讲解. 封装可以理解为隐藏一个类的成员变量和成员函数,只对外提供需要提供的成员函数. Java的封装主要通过访问权限控制符:priva ...
- STL set使用例子
#include<iostream>#include<set>using namespace std; #include<stdlib.h> #define ran ...
- vs2010连接postgresql数据库
Windows环境C/C++访问PostgreSQL主要有两种方式:利用Qt封装的数据库访问组件.利用PostgreSQL的API函数.使用Qt平台访问PostgreSQL的局限性很大,一旦脱离了访问 ...