319. Bulb Switcher——本质:迭代观察,然后找规律
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.
Subscribe to see which companies asked this question
class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
n=8
0,00000000
1,11111111
2,10101010
3,10001110
4,10011111
5,10010111
6,10010001
7,10010001
8,10010000=>2
bit 1=>1
bit 2=>0
bit 3=>0
bit 4=>1,2,4=>1
bit 5=>1,5=>0
bit 6=>1,2,3,6=>0
bit 7=>1,7=>0
bit 8=>1,2,4,8=>0
bit 9=>1,3,9=>1
bit 10=>1,2,5,10=>0
bit 11=>1,11=>0
bit 12=>1,2,3,4,6,12=>0
bit 13=>1,13=>0
bit 14=>1,2,7,14=>0
bit 15=>1,3,5,15=>0
bit 16=>1,2,4,8,16=>1 only if bit n is a square number
"""
ans = 0
i = 1
while i*i <= n:
i += 1
ans += 1
return ans
319. Bulb Switcher——本质:迭代观察,然后找规律的更多相关文章
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- LeetCode 319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 319. Bulb Switcher
题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ev ...
- 319. Bulb Switcher (Math, Pattern)
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 319 Bulb Switcher 灯泡开关
初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡切换一次开关. 第 3 轮,每三个灯泡切换一次开关(如果关闭,则打开,如果打开则关闭).对于第 i 轮,你每 i 个灯 ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
随机推荐
- CUBRID学习笔记 3 net连接数据库并使用cubrid教程示例
接上文 数据库安装好后,也可以测试语句了. 下面我们用c#写一个控制台程序,连接数据库,并读取数据. 一 下载驱动 net版的下 CUBRID ADO.NET Data Provider 9.3.0 ...
- python_way day11 自定义线程池
python_way day11 线程池 为什么需要线程池 线程多一些固然好,但是过多的线程反倒影响系统的负荷,所以我们就需要创建合适多的线程,哪我们把线程放到哪里?这时就放到线程池中. 线程池中存放 ...
- js中的 !!
就是这样:!!variable.哈哈,其实我也是在代码里面看见别人这样用,当时很好奇,所以就搜了一下,哈哈.还真的有很多相关的好文啊.作者是这样说的, 一般用来将后面的表达式转换为布尔型的数据 是不是 ...
- python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
- TCP三次握手及四次挥手详细 转
一.TCP报文格式 TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字段需要重点介绍下: ...
- Android_相关路径
1. Android应用安装涉及到如下几个目录:system/app 系统自带的应用程序,无法删除.data/app 用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.da ...
- BUAA & ICT 夏令营之旅
我还只有二十几岁,总应该相信点什么吧. ================================ 7.10午后坐火车赶到北京.一路上火车行驶在茫茫云海里.车窗外的世界是这样子的:一片广袤的原野 ...
- OpenWrt网络结构
原文链接:http://www.freezhongzi.info/?p=104 OpenWrt网络结构 OpenWrt的网络配置很丰富,在我看来几乎可以完成任何网络结构.下图为一个支持OpenWrt的 ...
- js 高程(三)学习感言(随时更新)
1.读第二遍了,感觉第一遍白读了. 2.现在还处于初学...
- Integer封装与拆箱
Integer封装与拆箱 简介: 目录: Integer自动封装的陷阱 Integer自动拆箱机制 Integer自动封装的陷阱 public class IntegerDemo { public s ...