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个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
随机推荐
- 苹果原生NSURLSession的上传和下载
关于NSURLSession的上传和下载 在iOS7.0后,苹果公司新推出了一个NSURLSession来代替NSURLConnection.NSURLConnection默认是在 主线程执行的.而N ...
- C语言程序设计现代方法_数组(第八章)
C语言不要求检查下标的范围.当下标超出范围时,程序可能会执行不可预知的行为. 看下这个程序: #include <stdio.h> #define N 10 // int main() { ...
- XAF应用开发教程(七)外观控制模块
很多时候,我们需要按照不同的条件显示不同的效果,在传统的软件开发中,我们会直接使用 控件名称.BackColor,Enable,Visible等属性进行控制. 如果一个业务对象在多处使用,要么我们会去 ...
- [转] Git SSH Key 生成步骤
Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. github的SSH配置如下: 一 . 设置Git的user name和email: $ git ...
- [LED]如何配置LCD背光和LED,调试方法
[DESCRIPTION] 如何配置LCD背光和LED,调试方法 [SOLUTION]LCD背光和LED配置文件alps/custom/<proj name>lk/cust_leds.ca ...
- (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高.所用到的工具或类库有 T ...
- 升级MySQL支持utf8mb4字符集详细步骤
原文:http://lib.csdn.net/article/mysql/4607 第一步:全备份所有数据库 [root@openfire1 mysql]# mysqldump -u root -p ...
- 转:c++内存分配
第一篇: http://my.oschina.net/pollybl1255/blog/140323 BSS段:(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS ...
- final简介
final简介 final用来修饰方法和属性表示特殊的意义.修饰方法时表示方法不能被重写:修饰属性时表示属性不能被改变,这里属性又分为对象和基本类型,修饰基本类型表示基本类型赋值以后不能再被赋值,修饰 ...
- 检测访问网页的浏览器呈现引擎、平台、Windows操作系统、移动设备和游戏系统
/** * Author: laixiangran. * Created by laixiangran on 2015/12/02. * 检测访问网页的浏览器呈现引擎.平台.Windows操作系统.移 ...