问题描述:

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. 【做题】CFedu41G. Partitions——推式子

    实际上这题的题面还是颇有意思,对两个划分不同的定义暗示了第二类斯特林数,模数是\(1000000007\)又表明这题不是NTT. 那么一开始的想法是考虑每个集合的贡献.设这个集合为\(S\),那么它的 ...

  2. 安装ubuntu的坑&RHEL7配置

    1.需要其他设置->分区,分区需要有/根目录分区和swap空间,后者文件系统类型选择swap,其他都是ext4 2.普通配置电脑,安装16.04.5 LTS,不要安装最新的,安装重启后卡在那里, ...

  3. oracle 之 安装后pl/sql登录报ora-12154

    这个问题一开始困扰了很久. 查的资料是复制一小段代码到tnsnames.ora中 SID名 = (DESCRIPTION =    (ADDRESS = (PROTOCOL = TCP)(HOST = ...

  4. 浅谈FFT、NTT和MTT

    前言 \(\text{FFT}\)(快速傅里叶变换)是 \(O(n\log n)\) 解决多项式乘法的一个算法,\(\text{NTT}\)(快速数论变换)则是在模域下的,而 \(\text{MTT} ...

  5. Kubernetes之Controllers三

    StatefulSets StatefulSet is the workload API object used to manage stateful applications. Note: Stat ...

  6. shell script 脚本编程

    介绍 Shell脚本编程30分钟入门 Shell 教程 Linux 的 Shell 种类众多,常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again ...

  7. HDU 4821 String(BKDRHash)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...

  8. python学习 day017打卡 类与类之间的关系

    本节主要的内容: 1.依赖关系 2.关联关系,组合关系,聚合关系 3.继承关系,self到底是什么? 4.类中的特殊成员 一.类与类之间的依赖关系 在面向对象的世界中,类与类中存在以下关系: 1.依赖 ...

  9. springmvc通过ajax异步请求返回json格式数据

    jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...

  10. sql 查询字段是中文/英文/数字 正则表达式

    一.包含中文字符 select * from 表名 where 列名 like '%[吖-座]%' 二.包含英文字符 select * from 表名 where 列名 like '%[a-z]%' ...