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 }
  • 相关阅读:
    Ios国际化翻译工具
    软件是什么
    angular2实现图片轮播
    DIV+CSS布局最基本的内容
    angular2中使用jQuery
    如何在Ionic2项目中使用第三方JavaScript库
    Ionic2项目中使用Firebase 3
    Ionic2中ion-tabs输入属性
    The Router路由
    templating(模板)
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/10823262.html
Copyright © 2011-2022 走看看