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个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
随机推荐
- JavaScript删除-confirm
一> onclick="javascript:if (confirm('您确定要删除吗?注意:此操作不可恢复,请谨慎操作!')){return true;} return false; ...
- 图文解说 Dijkstra.
Dijkstra 太多文章了,有些简练,有些一笔带过.自己还是花了些时间才明白,刚脆自己写个图文说明的,希望能让还没明白的,尽快清楚. 问题:求某点到图中其他所有点的最短路径(权值和最小) Dijks ...
- 9月java货车版速记
运算符的优先级java自带的方法正则表达式数组和二维数组:数组遍历,填充数组,数组排序,复制数组,数组查询数组算法:冒泡,选择,反转,快速类和对象:封装,继承,多态,this关键字,抽象类和接口重写和 ...
- typeof升级版,可以识别出array、object、null、nan、[]、{}
typeof 经常混淆array.object.null等,升级处理一下. 可以将这个函数放在common.js中使用. function getTypeName(v) { var v_str = J ...
- Java中的AWT进阶
围棋 package ch11; /** * Created by Jiqing on 2016/12/4. */ import java.awt.*; import javax.swing.*; i ...
- unsigned char 无符号整形 减法运算
对于一个字节来说: unsigned char : 0 ~ 255 0000 0000 ~ 1111 1111 char :-128 ~ 127 ...
- Android日常开发60条经验
1. 全部Activity可继承自BaseActivity,便于统一风格与处理公共事件,构建对话框统一构建器的建立,万一需要整体变动,一处修改到处有效. 2. 数据库表段字段常量和SQL逻辑分离,更清 ...
- 解决由于一个软件限制策略的阻止,windows无法运行此程序cmd.reg
解决由于一个软件限制策略的阻止,windows无法运行此程序cmd.reg Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFT ...
- jsp 页面 jstl 日期的计算
1. <% page import ="java.util.Date"%> 2. <% Date date = new Date(); date = new Da ...
- xcode6 framework missing submodule xxx 警告
xcode6 framework missing submodule xxx 警告 从xcode6开始,iOS可以直接创建生成framework了 如: 创建 framework 项目,TFKit.f ...