(java) Bulb Switcher
来源:互联网 发布:网络营销策划方案 论文 编辑:程序博客网 时间:2024/06/05 02:37
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.
思路:通过题意可以分析得出,只有是平方数的灯才不会被关掉,而小于等于一个的平方数个数有(int)math.sqrt(n)个。
代码如下(已通过leetcode)
public class Solution { public int bulbSwitch(int n) { return (n>=0?(int)Math.sqrt(n):0); } }
0 0
- (java) Bulb Switcher
- (java)Bulb Switcher
- LeetCode 319 -Bulb Switcher ( JAVA )
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- Bulb Switcher
- [LeetCode]-319. Bulb Switcher (Medium)(Java)
- 【Leetcode】:319. Bulb Switcher 问题 in JAVA
- 319. Bulb Switcher && 672. Bulb Switcher II
- [leetcode] 319. Bulb Switcher
- leetcode Bulb Switcher
- 9. Palindrome Number
- Android Studio ——在不root手机的情况下读取Data目录下的文件
- Java使用之坑货总结
- 225. Implement Stack using Queues
- 懒人公约数
- (java) Bulb Switcher
- Ultimaker 2:整体性能出众的家用3D打印机
- update-manager 设置导致网络没了
- 2016——大数据版图
- C语言结构体内存对齐(计算结构体内存大小)
- 如何连接新浪sae共享数据库
- Codeforces Round #256 (Div. 2) D Multiplication Table (二分)
- Thinkpad 电池设置 (Ubuntu)
- 此证书的签发者无效