博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Bulb Switcher
阅读量:4708 次
发布时间:2019-06-10

本文共 1255 字,大约阅读时间需要 4 分钟。

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 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.

转:

A bulb ends up on iff it is switched an odd number of times.

Call them bulb 1 to bulb n. Bulb i is switched in round d if and only if d divides i. So bulb i ends up on if and only if it has an odd number of divisors.

Divisors come in pairs, like i=12 has divisors 1 and 12, 2 and 6, and 3 and 4. Except when i is a square, like 36 has divisors 1 and 36, 2 and 18, 3 and 12, 4 and 9, and double divisor 6. So bulb i ends up on if and only if i is a square.

So just count the square numbers.

1 public class Solution {2     public int bulbSwitch(int n) {3         return (int)Math.sqrt((double)n);4     }5 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/5092962.html

你可能感兴趣的文章
Swift 用Delegate和Block实现回调的Demo
查看>>
第二十五章补充内容 17位字段
查看>>
灰色预测
查看>>
css随笔
查看>>
基于自己封装的select下拉选择的省市区三级联动效果,兼容IE
查看>>
booking.sh
查看>>
机器学习--避免过度拟合 笔记
查看>>
Linux SSH 基于密钥交换的自动登录原理简介及配置说明
查看>>
mariadb connector bug
查看>>
KnockoutJs学习笔记(十二)
查看>>
Noip2018游记
查看>>
Oracle11g数据库在Win系统下的安装
查看>>
初识Python
查看>>
nodejs+mysql入门实例(改)
查看>>
表达式语言
查看>>
jQuery EasyUI实现关闭全部tabs
查看>>
iOS项目之WKWebView替换UIWebView相关
查看>>
Lambda表达式效率问题
查看>>
【转载】iOS 设置Launch Image 启动图片(适用iOS9)
查看>>
最快得到MYSQL两个表的差集
查看>>