问题描述:

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. 1823: [JSOI2010]满汉全席 2-sat

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1823 思路 建图,缩点tarjan 判断impossible 代码 #include < ...

  2. 买不到的数目|2018年蓝桥杯A组题解析第八题-fishers

    买不到的数目 小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的两种.糖果不能拆包卖. 小朋友来买糖的时候,他就用这两种包装来组合.当然有些糖果数目是无法组合出来的,比如要买 10 颗糖 ...

  3. IDEA查看一个类的所有继承关系

    通常一个.java文件对应一个java类. 鼠标右击一个类: 即可查看.按住alt键可放大. 另一快捷键:光标在类名上,ctrl+H

  4. 【ASP.Net】 http请求中get,put,post,delete的区别与使用总结

    在web api的设计上, 需要设计这个每个action对应的资源的请求方法是什么. Get方法是对服务器资源的请求获取, 一般get方法的参数都放在URL当中的. 所以通常情况下这种请求方式都是不安 ...

  5. 【转载】C++宏定义详解

    摘自:http://blog.chinaunix.net/uid-21372424-id-119797.html   C++宏定义详解 2011-02-14 23:33:24   分类: C/C++ ...

  6. Latex: 添加IEEE会议论文作者信息

    参考: Multiple Authors with common affiliations in IEEEtran conference template Latex: 添加IEEE会议论文作者信息 ...

  7. Python: find the smallest and largest value

    题目要求: Write a program that repeatedly prompts a user for integer numbers until the user enters 'done ...

  8. Ubuntu下codeblocks不能自动缩进的问题

    如果在codeblocks中设置了自动缩进但是没有效果的话,在终端中执行sudo apt-get install codeblocks-contrib命令.

  9. js实现可视化区域内拖拽

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  10. Python pycharm 常用快捷键

    快捷键 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl + Shift + Enter 语句完成 ...