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. PIE SDK ISODATA分类

    1.算法功能简介 ISODATA(IterativeSelf-OrganizingDataAnalysisTechniqueAlgorithm)即迭代式自组织数据分析技术, 其大致原理是首先计算数据空 ...

  2. Linux-Xshell5

    Linux-Xshell5 1.下载 2.安装 3.新建会话(连接) 点击新建 需要知道要连接的 IP, 查看命令   ifconfig 配置 名称可以自己命名,主机写要连接的 IP,其他的不能改 输 ...

  3. 基于云计算的IaaS、PaaS、SaaS三种服务模式的区别

    Infrastructure-as-a-Service(IaaS) - 基础即设施服务 基础设施主要包括网络系统(networking).存储设备(storage).服务器(servers).虚拟化技 ...

  4. mac下安装win7虚拟机和导入linux虚拟机

  5. java语言编程使用正则表达式来实现提取(美团 3-5年经验 15-30k 北京 hadoop高级工程)中的3-5和15-30

    不多说,直接上干货! 如有这样的一条数据进来:   美团 3-5年经验 15-30k 北京 hadoop高级工程 //正则表达式提取工资值,因为15-30k后面有k,3-5年经验,不干净 public ...

  6. 案例41-hibernate练习-添加客户

    1 utils部分 1 HibernateUtils package www.test.utils; import org.hibernate.Session; import org.hibernat ...

  7. 线程同步(windows平台):互斥对象

    一:介绍 互斥对象是系统内核维护的一种数据结构,保证了对象对单个线程的访问权. 二:函数说明 创建互斥对象:    HANDLE CreateMutex(            LPSECURITY_ ...

  8. Beyond Compare 4试用期已过

    Beyond Compare 很好用,但是只有一段时间的试用时间,当试用期过了之后就提示不能试用了 怎么办呢? 我在网上找到了两个方法: 1.直接用注册码(来自:https://blog.csdn.n ...

  9. HDU 2795——Billboard——————【单点更新、求最小位置】

    Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  10. git clone时的各种报错汇总

    npm ERR! path E:\aawork\1work\2019.2\package.json 没有在项目路径下 npm ERR! missing script: dev 需要 vue init ...