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. 下厨房---map/字符串查询

    题目描述 牛牛想尝试一些新的料理,每个料理需要一些不同的材料,问完成所有的料理需要准备多少种不同的材料. 输入描述: 每个输入包含 1 个测试用例.每个测试用例的第 i 行,表示完成第 i 件料理需要 ...

  2. 分享个高精度IP定位

    https://www.opengps.cn/Data/IP/LocHighAcc.aspx

  3. 剑指offer——面试题9:用两个栈实现队列

    #include "Queue.h" // ====================测试代码==================== void Test(char actual, ...

  4. mysql PXC集群方案总结

    同时写集群内的所有机器 写性能依赖最慢的那个机器 读性能提高X倍

  5. SqlSugar ORM框架文档

    http://www.codeisbug.com/Doc/8/1141 SqlSugar入门级教程+实例 (.net core下的)https://www.cnblogs.com/rulasann/p ...

  6. stream4

    import java.util.Comparator; import java.util.function.BinaryOperator; public class BinaryOperatorTe ...

  7. oracle 操作实例(一)----rman 全备恢复

    一,环境背景 拥有全备数据库 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=pr ...

  8. kafaka安装

    wget https://mirrors.cnnic.cn/apache/kafka/2.0.0/kafka_2.11-2.0.0.tgz 解压 Tar -xvf kafka_2.11-2.0.0.t ...

  9. params传递任意参数

    namespace 传递任意参数{ class Program { static void Main(string[] args) { //可传递任意数量参数 Test(1, 2, "sas ...

  10. [Verilog] parameter

    parameter和localparam的作用范围均为本模块,区别在于前者可用于在实例化模块的时候进行参数的传递. 用已定义的参数对变量赋值时,按照补码的方式处理,若出现溢出的情况,则截取低位.