(java)Bulb Switcher
2016-05-16 16:34
447 查看
题意:(开灯关灯的问题,轮训)
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 ith round, you toggle every i bulb.
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.
(1)最后整理的思路:先N以内所有数的因子有多少个,如果是奇数个就count++;
public static int bulbSwitch(int n) {
int[] temp=new int[n+1];
for(int i=1;i<=n;i++){
for(int j=2;i*j<=n;j++){
temp[i*j]++;
}
}
int count=0;
for(int i=1;i<=n;i++){
if(temp[i]%2==0){
count++;
}
}
return count;
}
(2)超时,进而思路:N以内只有是能开方的数的因子才是奇数个,所以求N以内能开方(完全平方数比如4,9,16......)的数的个数就行
public class Solution {
public int bulbSwitch(int n) {
int count=0;
for(int i=1;i<=n;i++){
if(i==(int)Math.sqrt(i)*(int)Math.sqrt(i)){
count++;
}
}
return count;
}
}
(3)超时,进而思路,,直接求个数(网上更简洁的方式是return Math.sqrt(N))
public class Solution {
public int bulbSwitch(int n) {
int count=0;
for(int i=1;i*i<=n;i++){
count++;
}
return count;
}
}
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 ith round, you toggle every i bulb.
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.
(1)最后整理的思路:先N以内所有数的因子有多少个,如果是奇数个就count++;
public static int bulbSwitch(int n) {
int[] temp=new int[n+1];
for(int i=1;i<=n;i++){
for(int j=2;i*j<=n;j++){
temp[i*j]++;
}
}
int count=0;
for(int i=1;i<=n;i++){
if(temp[i]%2==0){
count++;
}
}
return count;
}
(2)超时,进而思路:N以内只有是能开方的数的因子才是奇数个,所以求N以内能开方(完全平方数比如4,9,16......)的数的个数就行
public class Solution {
public int bulbSwitch(int n) {
int count=0;
for(int i=1;i<=n;i++){
if(i==(int)Math.sqrt(i)*(int)Math.sqrt(i)){
count++;
}
}
return count;
}
}
(3)超时,进而思路,,直接求个数(网上更简洁的方式是return Math.sqrt(N))
public class Solution {
public int bulbSwitch(int n) {
int count=0;
for(int i=1;i*i<=n;i++){
count++;
}
return count;
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树