zoukankan      html  css  js  c++  java
  • [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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after nrounds.

    Example:

    Input: 3
    Output: 1 
    Explanation: 
    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.

    题意

    Solution1

    code

     1 /**
     2 factor of 6: 1,2,3,6
     3 factor of 7: 1,7
     4 factor of 9: 1,3,9
     5 
     6 so all number have even number of factors except square number(e.g: factor of 9:1,3,9).
     7 square number must turn on because of odd number of factors(9: turn on at 1st, off at 3rd, on at 9th)
     8 other number must turn off(6: turn on at 1st, off at 2nd, on at 3rd, off at 6th)
     9 so we only need to compute the number of square number less equal than n
    10 **/
    11 
    12 public class Solution {
    13     public int bulbSwitch(int n) {
    14         return (int)Math.sqrt(n);
    15     }
    16 }
  • 相关阅读:
    模块
    javacript一键换肤
    ajax请求
    jquery获取元素的方法
    ajax请求里的contentType: "application/json"作用
    Underscore.js 入门-常用方法介绍
    .NET Core调用WCF的最佳实践
    证伪主义——科学与伪科学的量尺
    高效的筒仓
    此时此刻,一个项目正在走向失败
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/10823262.html
Copyright © 2011-2022 走看看