问题描述:

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

问题分析:

/**
*
* 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 nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
*
* 一、从给定的题目中找到规律:就是,当 i 为 1 到 n之间的任何数时,翻转 k = ri 位置上灯泡的状态。
* 求n轮之后,还有几盏灯是开着的?
* 二、最直观的方法是穷举法:一轮轮的遍历,但是,当n很大时,时间复杂度太大。
* 三、找规律:
* 若起初有5盏灯,经过5轮翻转后,最终只有第1盏和第4盏是亮着的,不妨分析下,这里面是否有什么规律?
* 起始:5盏灯全部为 off
* 1 = (1,1) 1次翻转,最终on
* 2 = (1,2),(2,1) 2次翻转,最终off
* 3 = (1,3),(3,1) 2次翻转,最终off
* 4 = (1,4),(2,2),(4,1) 3次翻转,最终on
* 5 = (1,5),(5,1) 2次翻转,最终off
*
* 可以看出,最终为on的灯所在位置都是平方数
* @author admin
*
*/

代码:1到n之间 平方数的个数 为: sqrt(n) 向下取整。

//返回n轮之后,有多少盏灯是开着的
public static int bulbSwitch(int n){
if(n <= 0)
return 0;
int num = (int)Math.sqrt(n); //求n的平方根,double强制转换为int类型,即为向下取整。 System.out.println(num); return num;
}

Bulb Switcher (leetcode java)的更多相关文章

  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 解题报告(Python)

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

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

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

  5. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  6. 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 ...

  7. Java [Leetcode 319]Bulb Switcher

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

  8. LeetCode Bulb Switcher 319

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

  9. Leetcode Bulb Switcher

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

随机推荐

  1. Visual Question Answering with Memory-Augmented Networks

    Visual Question Answering with Memory-Augmented Networks 2018-05-15 20:15:03 Motivation: 虽然 VQA 已经取得 ...

  2. 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos

      End-to-End Learning of Action Detection from Frame Glimpses in Videos  CVPR 2016  Motivation:    本 ...

  3. ORM之EF

    本文大部分内容截取自博客:  http://www.cnblogs.com/VolcanoCloud/p/4475119.html (一) 为什么用ORM 处理关系数据库时,我们依据由行和列组成的表, ...

  4. Wordpress搭建

    Install Environment apt install apache2 php mysql-server apt install php-mysql php-fpm Config mysql ...

  5. PHP 内置函数fgets读取文件

    php fgets()函数从文件指针中读取一行 语法: fgets(file,length) 参数 描述 file  必需.规定尧要读取的文件 length 可选 .规定尧都区的字节数.默认是102字 ...

  6. Git 常用使用技巧

    1.创建代码仓库 Step 1:先配置下我们的身份吧,这样在提交代码的时候Git就可以知道是谁提交的,命令如下: git config --global user.name "coder-p ...

  7. 关于页面的跳转添加参数(比如id啥的)

    最近一些新手老是问我一些关于页面之间的传递跳转,怎么带参数这件事,(比如说一个列表页到详情页面数据是怎么传输的),今个没事就在这说一些! 这里拿列表页面到详情页面来阐述下哈! 首先在列表页,我们通过a ...

  8. Codeforces 786 A. Berzerk

    题目链接:http://codeforces.com/problemset/problem/786/A 这个题出做$DIV2$的$C$以及$DIV1$的A会不会难了一点啊... 做法和题解并不一样,只 ...

  9. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...

  10. Django模板操作

    进行加减运算 def index(request): a = request.GET['a'] b = request.GET['b'] c = int(a) + int(b) return Http ...