美文网首页每日打卡
2021-11-15 319. 灯泡开关

2021-11-15 319. 灯泡开关

作者: 16孙一凡通工 | 来源:发表于2021-11-15 09:41 被阅读0次

模拟思路解这道题,存在着复杂度高的缺陷,因此在测试用例n=99999时,垮掉了,题解上也说明即使O(n)最终执行用时也是1400ms,很大,因此看到官方题解采用了数学的思路。

java版本

class Solution {
    boolean[] arr;
    public int bulbSwitch(int n) {
        if(n==0 || n==1){
            return n;
        }
        
        int tmp;
        arr=new boolean[n];

        for(int i=2;i<=n;i++){
            tmp=i;
         changeBulk(arr,tmp);
        }
        int count=0;
         for(int i=0;i<arr.length;i++){
            //  System.out.println(arr[i]);
          if(arr[i]==false){
              count++;
          }
         }
        return count;
    }
    public void changeBulk(boolean[] arr,int tmp){
        int count=0;
        for(int i=0;i<arr.length;i++){
            count++;
            if(count==tmp){
                arr[i]=!arr[i];
                count=0;
            }
        }
    }
}

Go版本:

func bulbSwitch(n int) int {

    return int(math.Sqrt(float64(n)+0.5))
  
 
}

相关文章

  • Leetcode-319 灯泡开关

    319. 灯泡开关[https://leetcode-cn.com/problems/bulb-switcher/...

  • Leetcode【319、672】

    问题描述:【Math】319. Bulb Switcher 解题思路: 灯泡开关。初始时有 n 个灯泡关闭,第 i...

  • LeetCode 319. 灯泡开关

    1、题目 319. 灯泡开关 - 力扣(LeetCode) https://leetcode-cn.com/pro...

  • 2021-11-15 319. 灯泡开关

    模拟思路解这道题,存在着复杂度高的缺陷,因此在测试用例n=99999时,垮掉了,题解上也说明即使O(n)最终执行用...

  • LeetCode 319. 灯泡开关

    题目 319. 灯泡开关 题目描述 初始时有 n 个灯泡关闭。 第 1 轮,你打开所有的灯泡。 第 2 轮,每两个...

  • 319. 灯泡开关

    初始时有 n 个灯泡关闭。 第 1 轮,你打开所有的灯泡。 第 2 轮,每两个灯泡你关闭一次。 第 3 轮,每三个...

  • 设计模式 · 开关和灯泡的问题

    一、 假设我们现在有一个开关(打开和关闭)、还有一个灯泡(发光和不发光),开关控制灯泡,当开关打开后,灯泡亮。思考...

  • 灯泡开关

    灯泡开关 在这个问题中,我们能够首先想到的就是使用暴力模拟。根据模拟可以直接模拟每一步的操作。但是这会发生TLE错...

  • 灯泡开关

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/bulb-s...

  • 简单电路图

    原理:在灯泡的两端加上一定的电压,灯泡就可以发光。 材料 灯泡,底座,电池,导线,开关。 图片

网友评论

    本文标题:2021-11-15 319. 灯泡开关

    本文链接:https://www.haomeiwen.com/subject/lsnftrtx.html