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 ith round, you toggle every i bulb. 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.

分析:http://www.programcreek.com/2014/05/leetcode-bulb-switcher-java/

By using some examples we can find out the number of switches for each bulb:

1 -> 1 (1)
2 -> 2 (1 2)
3 -> 2 (1 3)
4 -> 3 (1 2 4)
5 -> 2 (1 5)
6 -> 4 (1 2 3 6)
7 -> 2 (1 7)
8 -> 4 (1 2 4 8)
9 -> 3 (1 3 9)

So only (i*i)th element has odd number of switches and they are on. The problem is now get all the square numbers.

 public class Solution {
public int bulbSwitch(int n) {
return (int)Math.sqrt(n);
}
}

最直接的方法:

 public class Solution {
public int bulbSwitch(int n) {
int count = ;
for (int i = ; i <= n; i++) {
int numSwitch = helper(i);
if (numSwitch % == )
count++;
} return count;
} public int helper(int kthBulb) {
int count = ;
for (int i = ; i <= kthBulb; i++) {
if (kthBulb % i == )
numSwitch++;
}
return numSwitch;
}
}

Bulb Switcher的更多相关文章

  1. [LeetCode] Bulb Switcher 灯泡开关

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

  2. [LeetCode] Bulb Switcher II 灯泡开关之二

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

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

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

  4. LC 672. Bulb Switcher II

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

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

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

  6. LeetCode Bulb Switcher 319

    变换灯泡颜色 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  7. Leetcode Bulb Switcher

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

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

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

  9. Java [Leetcode 319]Bulb Switcher

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

随机推荐

  1. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  2. Java.util.ArrayList详解

    java.util.ArrayList就是传说中的动态数组. 继承了关系,有此可看出ArrayList与list的collection的关系 public class ArrayList<E&g ...

  3. str内部方法

    代码 #str内部功能 name=' aK am\til.L iu' age=18 num=-11 ab='#' ac=('1','2','3','4','5','6','7') print(dir( ...

  4. groovy-输入输出

    Groovy为I/O提供了一系列的helper methods ,所有的这些方法都适用于标准的 Java Reader/Writer ,InputStream/OutputStream 和File 以 ...

  5. Linux 基础网络设置

    一.查看以及测试网络 查看及测试网络配置是管理Linux网络服务的第一步,本节将学习Linux系统中的网络查看以及测试命令.其中讲解的大多数命令以普通用户权限就可以完成操作,但是普通用户在执行&quo ...

  6. codeforce626D (概率)

    D. Jerry's Protest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. mysql语句分析

    explain的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 1,id   select识别符.这是select的查询序列号.2,select_type 可以为一下任何一种类型simple ...

  8. shared_ptr<> reset

    // std_tr1__memory__shared_ptr_reset.cpp // compile with: /EHsc #include <memory> #include < ...

  9. 如何获取xcassets中LaunchImage图片

    NSDictionary * dic = @{@"320x480" : @"LaunchImage-700", @"320x568" : @ ...

  10. XSS Filter Evasion Cheat Sheet 中文版

    前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在 ...