LeetCode 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. 题目大意:给你n个灯泡编号1~n,开始都是关着的,在第i轮,将编号为i的倍数的灯切换状态(开->关、关->开)。问你n轮后灯亮着的有多少个。 解题思路:想到了对于每个灯泡,如果它的因子个数为奇数,那么它必然会是开着的。但是这还不是最机智的做法,更近一步去思考,你会发现,x的因子满足 p*q == x,所以进而转化成只要是完全平方数,那么就会是亮着的。
class Solution {
public:
int bulbSwitch(int n) {
int ret = 0;
for(int i = 1; i*i <= n; i++){
ret++;
}
return 0;
}
};
LeetCode 319 ——Bulb Switcher——————【数学技巧】的更多相关文章
- 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灯泡开关
智商压制的一道题 这个题有个数学定理: 一般数(非完全平方数)的因子有偶数个 完全平凡数的因子有奇数个 开开关的时候,第i个灯每到它的因子一轮的时候就会拨动一下,也就是每个灯拨动的次数是它的因子数 而 ...
- Leetcode 319 Bulb Switcher 找规律
有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭. class Solution { public: int bulbSwitch(int n) { return (int)sqrt( ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- 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——本质:迭代观察,然后找规律
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- 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 个灯 ...
随机推荐
- 【C#】MVC+EF+LINQ 综合小项目
第一,创建数据库 create table category(id int primary key,name nvarchar(20)) create table news(id int primar ...
- WPF程序开机速度策略
WPF程序开机速度慢是一个很讨厌的问题.具体分析后,可能有以下问题 1.在主线程中加载图像导致 2.初始化各种UserControl导致 3.加载类库导致
- (一)用C或C ++扩展(翻译)
用C或C ++扩展 如果你知道如何用C语言编程,那么为Python添加新的内置模块是很容易的.这种扩展模块可以做两件不能直接在Python中完成的事情:它们可以实现新的内置对象类型,以及调用C库函数和 ...
- C# 多态(2)
接上面讲 我们知道虚方法,和重写方法. 但是有没有发现 override和new的作用是差不多的. 但为什么还要单独写出来两个呢? 首先,咱们要明白一个问题,继承是具有线性传播的. class Fat ...
- logback&slf4j学习笔记
1.Slf4j 1.1.Slf4j简介 SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.实际上,SLF ...
- 使用github和hexo搭建静态博客
获得更多资料欢迎进入我的网站或者 csdn或者博客园 终于写这篇文章了,这是我使用github和hexo搭建博客的一些心得,希望能给大家一点帮助.少走点弯路.刚接触github,只是用来存项目的版本, ...
- 最短路径SPFA算法(邻接表存法)
queue <int> Q; void SPFA (int s) { int i, v; for(int i=0; i<=n; i++) dist[i]=INF; //初始化每点i到 ...
- 分享记录一批免费VIP视频解析接口,不定时更新!
VIP视频接口的作用相信大家都懂,那么,由于接口的维护.开发具有不稳定性,失效率很高.这里收集一些目前可用的接口,如果不能用,请反馈给我删除,感谢大家! 电影<西虹市首富>优酷链接:htt ...
- Docker 安装Hadoop集群
资源准备:jdk1.8及hadoop2.7.3 链接:https://pan.baidu.com/s/1x8t1t2iY46jKkvNUBHZlGQ 提取码:g1gm 复制这段内容后打开百度网盘手机A ...
- error while loading shared libraies :libopencv_core_so.3.4:cannot open shared object
TX2 上安装自己编译的opencv,使用时出现: error while loading shared libraies :libopencv_core_so.3.4:cannot open sha ...