【LeetCode】319. Bulb Switcher 解题报告(Python)
【LeetCode】319. Bulb Switcher 解题报告(Python)
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/bulb-switcher/description/
题目描述:
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.
题目大意
n个灯泡,起始状态是灭的,需要按照1的倍数,2的倍数,3的倍数……的位置打开关闭n次,求最后亮了几个。
解题方法
真的是智力题,暴力解法超时,最后的结果是开平方就行。
对于这个题,在http://blog.csdn.net/baidu_23318869/article/details/50386323上有详细的解释,应该能看懂。就不多说了。
代码:
class Solution(object):
def bulbSwitch(self, n):
"""
:type n: int
:rtype: int
"""
return int(math.sqrt(n))
日期
2018 年 3 月 5 日
【LeetCode】319. Bulb Switcher 解题报告(Python)的更多相关文章
- LeetCode 319 ——Bulb Switcher——————【数学技巧】
319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...
- 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 ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
- [LeetCode]319. Bulb Switcher灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- 01 eclipse搭建maven的web工程(3.1)
eclipse搭建maven的web工程(3.1) 一.下载并在eclipse安装JDK环境[查看] 二.下载并在eclipse安装maven环境[查看] 三.新建maven-webapp工程: 1. ...
- day14搭建博客系统项目
day14搭建博客系统项目 1.下载代码包 [root@web02 opt]# git clone https://gitee.com/lylinux/DjangoBlog.git 2.使用pid安装 ...
- 在 windows 系统上 安装与配置 PHP + Apache
参考:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在大学时候上过一门PHP课时,因为课堂需要配置过一次PHP+Mysql ...
- Java中特殊的类——Object类
Java中特殊的类--Object类 1.Object类的概述 Object类是java默认提供的类.Java中除了Object类,所有的类都是有继承关系的.默认会继承Object类,即所有的对象都可 ...
- Centos 7 安装redis,修改配置文件不生效、外网不能访问。
前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...
- jQuery 的两种语法
文档就绪事件(文档加载完成之后才执行jQuer代码): 第一种: $(document).ready(function() { // jQuery 代码.... }); 第二种: $(function ...
- 【C/C++】最长不下降子序列/动态规划
#include <iostream> #include <vector> using namespace std; int main() { //输入 int tmp; ve ...
- 【C/C++】习题3-5 谜题/算法竞赛入门经典/数组和字符串
[题目] 有一个5*5的网络,恰好有一个格子是空的(空格),其他格子各有一个字母. 指令:A, B, L, R 把空格上.下.左.右的相邻字母移到空格中. [输入] 初始网格和指令序列(以数字0结束) ...
- .Net Core MVC全局过滤器验证是否需要登录
1.新增全局登录过滤器LoginCheckAttribute 1 public class LoginCheckAttribute: ActionFilterAttribute 2 { 3 publi ...
- Mysql配置文件 16c64g优化
目录 一.说明 二.配置 一.说明 以下配置适合16核64G及以上的配置,会让性能稍微提高1/3左右. 二.配置 my.cnf [client] port = 3306 socket = /usr/l ...