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. 将Eclipse的Java Project转换为Dynamic Web Project

    在用Eclipse做JavaEE开发时经常遇到Web工程被识别为Java工程的问题,导致很多功能无法使用. 只需做以下操作便可解决该问题. 1.右击Java工程选择Properties 2.选择左边目 ...

  2. 如何在64位WIN7下安装64位的解压版mysql-5.6.37-winx64.zip

    1.到mysql官网下载 https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.37-winx64.zip 2.将解压缩后的文件放到自己想要的地方, ...

  3. PIE SDK元素的保存与打开

    1.功能简介 绘制元素之后需要对元素进一步的保存操作,可以利用PIE SDK的ExportElementsCommand命令保存成xml格式的文件,打开元素可以利用ImportElementsComm ...

  4. Flutter编程:Flutter命令行的学习

    1.创建 Flutter 工程 flutter create <output directory> D:\notebook\flutter\projects\ui_tutorial\lay ...

  5. Django_Xadmin 修改后台

      admin组件使用 Django 提供了基于 web页面的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTA ...

  6. lua输入函数名字符串执行函数

    str = "testA()"loadstring(str)() function testA() ------end 使用loadstring即可执行后面在xlua用了下发现不能 ...

  7. 守护客户数据价值:企业级NewSQL HTAP分布式云TBase架构详解

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:jasonys,隶属于腾讯技术工程事业群数据平台部,负责TBase数据的技术研发和架构设计,有超过10年的数据库内核开发设计经验,完成 ...

  8. Programmer Competency Matrix--ref--http://sijinjoseph.com/programmer-competency-matrix/

    Note that the knowledge for each level is cumulative; being atlevel n implies that you also know eve ...

  9. SQLAlchemy基本操作和常用技巧

    点击打开链接 Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量实例,非常好的一个学习SQLAlchemy的教程,需要的朋友可以参考下 python编程语言下的一款开源软件.提供 ...

  10. CSS+DIV进度条

    <style type="text/css"> .Bar { position: relative; width: 200px; /* 宽度 */ border: 1p ...