【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 日

最新文章

  1. 封装系统(以封装Windows 7为例)
  2. a letter and a number
  3. redis 安装及配置
  4. C# static 干货全解析
  5. IOS 开展 分别制定了iphone 和 ipad 好? 或开发一个 Universal好?
  6. nginx 引入外部文件
  7. html_web存储
  8. js 常用正则表达式表单验证代码
  9. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(最大生成树)
  10. 使用筛选器特性标记方法解决webapi 跨域问题
  11. MYSQL的group by笔记
  12. git ignore 总结
  13. python--第十七天总结(Django)
  14. java中的Sort函数,你值得看
  15. [日常] 编写HTTP接口文档
  16. C语言位域——精妙使用内存
  17. Android APP分享功能实现
  18. [hihoCoder] 骨牌覆盖问题·二
  19. 本地Jdev Run PG报严重: Socket accept failed错误
  20. 机器学习实战笔记(Python实现)-07-模型评估与分类性能度量

热门文章

  1. 浏览器点击URL的响应过程
  2. SIG -MESH -1
  3. SpringBoot 整合 MyBatis,实现 CRUD 示例
  4. C#gridview尾部统计
  5. Hadoop入门 集群崩溃的处理方法
  6. 零基础学习java------day4------流程控制结构
  7. Linux学习 - 挂载命令
  8. Spring.DM版HelloWorld
  9. java职业路线图
  10. jdk1.7源码之-hashMap源码解析