- 浏览: 173947 次
- 性别:
- 来自: 济南
文章分类
最新评论
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等于10,N代表on, F代表off,这个执行的过程如下:
从中可以看出只有1,4, 9 三个灯泡被toggle了偶数次(把所有灯打开之后的次数), 其余都是被toggle了奇数次,因此我们只需要得到灯泡个数中有几个平方数就可以了。代码很简单:
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等于10,N代表on, F代表off,这个执行的过程如下:
从中可以看出只有1,4, 9 三个灯泡被toggle了偶数次(把所有灯打开之后的次数), 其余都是被toggle了奇数次,因此我们只需要得到灯泡个数中有几个平方数就可以了。代码很简单:
public class Solution { public int bulbSwitch(int n) { return (int) Math.sqrt(n); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 229You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 344Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 335Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 453Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 514Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 431Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 622Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 430The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 520Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 535Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 863Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 557Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 603Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 766Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 737You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 631For a undirected graph with tre ...
相关推荐
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
leetcode 530 力扣在线评委 ...Switcher 343. Integer Break 268. Missing Number 144. Binary Tree Preorder Traversal 2016.06.06 94. Binary Tree Inorder Traversal 318. Maximum Product of Word Length
LightBulb 1.6.3.6 便携版 - 蓝光过滤器、降低屏幕色温,保护眼睛 使用步骤: 1. 首先安装 .net framework 4.5.2 2. 双击 Manual_Gamma_Registry_Fix.reg 导入注册表,然后重启电脑 (如果已导入过,则此步骤可...
LightBulb 是一个屏幕色温调节软件和蓝光过滤器,可以减少蓝光输出,达到护眼效果。它与著名的 f.lux 是同类型软件,不过 LightBulb 是一个开源软件。对于经常枯坐与电脑面前办公的朋友来说,这款软件可以让降低眼...
LightBulb 是一个屏幕色温调节软件和蓝光过滤器,可以减少蓝光输出,达到护眼效果,它与著名的 f.lux 是同类型软件,不过 LightBulb 是一个开源软件。 LightBulb 能够根据日出日落时间,自动调整屏幕颜色,特别是在...
趣味创意,使用Qt贴图实现美观的辉光管电子钟,从中可以学习布局、QLCDnumber、资源文件的使用等等
GitHub开源护眼小工具,妈妈再也不用担心我的眼睛被蓝光伤害了 说明贴:https://blog.csdn.net/qq_45237181/article/details/119059636
Bulb_Q.SLDPRT
LightBulb是一个开源的python框架用于审计web应用程序防火墙
VSCode :light_bulb: 用于neovim的内置LSP。 gif中显示的代码操作选择窗口是 简介/理论 只要当前光标位置处有textDocument/codeAction ,插件就会在符号栏中显示一个灯泡。 这使得代码动作既可,因为即使没有可见...
USCAR-15 Specification for Testing Automotive Miniature Bulb Socket_Circuit Plate Assemblie.pdf
LightBulb一个后台程序能够根据时间减少电脑蓝光
python库。 资源全名:bulb-core-0.0.56.tar.gz
LIFX-Bulb-Rave 使用 LIFX 灯泡打造狂欢派对! 这个简单的控制台应用程序可以捕获来自扬声器的声音,并使用指定的 LIFX-Bulb 创建狂欢派对! 由于仅适用于 Windows,因此该程序仅适用于 Windows :/(目前) #去做...
资源来自pypi官网。 资源全名:bulb-core-0.0.92.tar.gz
资源分类:Python库 所属语言:Python 资源全名:bulb-core-0.0.31.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Arduino-AI-Thinker_RGBW_Bulb.zip,人工智能思考者rgbw灯泡的替代固件,基于mqtt协议和人工智能思考者rgbw灯泡的tls连接替代固件,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的...
资源分类:Python库 所属语言:Python 资源全名:bulb-core-0.0.50.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python库。 资源全名:bulb_core-0.0.22-py3-none-any.whl