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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Input: 3
Output: 1
Explanation:
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.

Solution: find the pattern among them:

n       :  res

(1--3): 1

(4--8): 2

(9--15): 3

(16--24): 4

basically, sqrt(n).toint()

class Solution {
public int bulbSwitch(int n) {
if(n==0) return 0;
for(int i = 1; i<=Math.sqrt(n); i++){
if(n>=i*i && n<=(i+2)*i){
return i;
}
}
return 0;
}
}

319. Bulb Switcher (Math, Pattern)的更多相关文章

  1. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  2. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  3. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  4. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  5. 319. Bulb Switcher

    题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...

  6. 319. Bulb Switcher——本质:迭代观察,然后找规律

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  7. [LeetCode]319. Bulb Switcher灯泡开关

    智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...

  8. 319 Bulb Switcher 灯泡开关

    初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯 ...

  9. Leetcode 319 Bulb Switcher 找规律

    有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...

随机推荐

  1. my32_ error 1872 Slave failed to initialize relay log info structure from the repository

    重启了实例后,slave进程无法开启 Last_SQL_Errno: Last_SQL_Error: Slave failed to initialize relay log info structu ...

  2. rem.js的用法及在浏览器端的适配

    首先介绍下自己,我是从后端转前端,对于前端的见识还不深望各位全当一个新手自述....... 随着移动设配的更新换代,市场上涌现了大量的非主流设备分辨率,比如华为手机...... 更新换代快的大前提下自 ...

  3. 日志收集之filebeat

    一,软件介绍 Filebeat是一个轻量级日志传输Agent,可以将指定日志转发到Logstash.Elasticsearch.Kafka.Redis等中.Filebeat占用资源少,而且安装配置也比 ...

  4. django设置打印数据库日志

    在settings.py中添加: LOGGING = { 'disable_existing_loggers': False, 'version': 1, 'handlers': { 'console ...

  5. TOJ 3176 Challenge from XOR

    Description Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator ...

  6. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

  7. Spring3.2下使用JavaMailSenderImpl类发送邮件

    1.JavaMailSenderImpl类 Spring的邮件发送的核心是MailSender接口,在Spring3.0中提供了一个实现类JavaMailSenderImpl,这个类是发送邮件的核心类 ...

  8. js控制字符处理

    使用js在对json字符串转json对象时,如果遇到一些控制(特殊)字符会出现转化失败的情况 处理方法:通常我们可以把这些控制字符替换成空 function character(str) { retu ...

  9. Spring @Resource, @Autowired and @Inject 注入

    Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...

  10. BAT的java面试题

    公司:国内三巨头其中的一家!面试时间约在1月份左右!基本上都是在晚上,所以不影响白天上班! 一面 一面偏架构方面 1.介绍一下自己,讲讲项目经历 2.你们项目中微服务是怎么划分的,划分粒度怎么确定? ...