LeetCode Bulb Switcher 319
2016-01-20 14:08
176 查看
变换灯泡颜色
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:
按题意coding
运行超时:
灯泡颜色的变换只存在于自己的因子处
1.素数,只有1和自己。
2.普通数,因子成对出现。
3.只有完全平方数灯泡奇数次点亮不会熄灭。
此题转化为求完全平方数的个数!!!
比如:3-->1,4-->2,10-->3
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 ; 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); }
相关文章推荐
- Win10系统怎么锁屏才能让别人找不到登陆界面?
- opentsdb源码分析---Deferred
- Socket开发之通讯协议及处理(解决粘包问题)
- 浅析@synthesize与@dynamic的最本质区别
- 2016年初Android开源项目总结
- Lua 字符串操作
- 心得
- Java语言中String 、StringBuffer 、StringBuilder的区别
- whuoj1574 第K小数
- bzoj3781 小B的询问【莫队】
- Git命令详解
- android开源项目汇总
- iOS语法小结---豁然开朗
- ssh提示输入密码回显
- Spring的DAO异常-你可能忽视的异常
- springMVC访问静态资源的两种方式
- IOS-多线程(NSOperation)
- ContactsUI(修改联系人)
- linux常用命令—— 系统设置(七)
- 欢迎使用CSDN-markdown编辑器