关闭

leetcode 319 Bulb Switcher

171人阅读 评论(0) 收藏 举报
分类:

1 暴力解法(超时):

 

package leetcode;

public class Solution319 {
	public int bulbSwitch(int n) {
        if(n == 0){
            return 0;
        }
        boolean[] bulbs = new boolean[n];
        int count = 1;
        int numOfbulbs = 0;
        while(count <= n) {
            for(int i = count-1; i < n; i+=count) {
                if(bulbs[i]) {
                    bulbs[i] = false;
                }else{
                    bulbs[i] = true;
                }
            }
            ++count;
        }
        
        StringBuffer s = new StringBuffer(new String(""));  
        for(int i = 0;i < n; ++i){
            if(bulbs[i]){
                ++ numOfbulbs;
            }
            s.append(bulbs[i] ? 1 : 0);
        }
        System.out.println(s);
        return numOfbulbs;
    }
	
	public static void main(String[] args) {
		Solution319 solution319 = new Solution319();
		int n = 30;
		for(int l = 0;l < 30; ++l) {
			solution319.bulbSwitch(l);
		}
	}
}
2 找规律解法:

 打印输出出规律,然后再看具体的情况。

 code:

 

public class Solution319Lcq {
	public int bulbSwitch(int n) {
		if(n <= 3){
			return 1;
		}
		int wheel = 2;  
		int result = 0;
		int trackvehicle = 0;
		boolean isNotWheel = true;
		while(trackvehicle <= n){
			if(isNotWheel) {
				trackvehicle += 1;
				result += 1;
				//wheel;
				isNotWheel = !isNotWheel;
			}else{
				trackvehicle += wheel;
				wheel += 2;
				isNotWheel = !isNotWheel;
			}
		}
		return result;
	}
}

0
0
查看评论

LeetCode319——Bulb Switcher

这是一道智商碾压的题。。。
  • zhangxiao93
  • zhangxiao93
  • 2015-12-21 09:58
  • 2190

leetcode 672. Bulb Switcher II

There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operati...
  • huanghanqian
  • huanghanqian
  • 2017-09-05 22:34
  • 958

Leetcode 319 : Bulb Switcher

Leetcode–319Bulb 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 th...
  • Code_J_xer
  • Code_J_xer
  • 2016-07-18 08:45
  • 131

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 ...
  • wrsophia_977
  • wrsophia_977
  • 2016-07-28 08:12
  • 81

leetcode 319 Bulb Switcher

leetcode319 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 th...
  • u014236178
  • u014236178
  • 2015-12-21 23:54
  • 114

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...
  • niuooniuoo
  • niuooniuoo
  • 2016-05-02 17:53
  • 277

LeetCode 319 Bulb Switcher

题目   灯泡初始状态为off 每一轮将是n的倍数的开关调转状态。 求最后灯泡是on的个数。 思路:由于 1 = 1*1                     2 = 2...
  • weixin_40574455
  • weixin_40574455
  • 2017-11-01 16:54
  • 12

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 ...
  • happyxuma1991
  • happyxuma1991
  • 2016-04-22 21:27
  • 69

leetcode 319 - Bulb Switcher

leetcode题目汇集 —  Bulb Switcher leetcode 319 - Bulb Switcher 题目要求: There are n bulbs that are initially off. You first turn on all...
  • u011732614
  • u011732614
  • 2016-11-28 10:56
  • 70

Leetcode 319 - Bulb Switcher(Math)

题意点灯问题,有n个灯,进行n轮操作:第i轮,将i的所有整数倍灯的状态翻转,比如第2轮,将2,4,6…的灯的状态翻转。初始状态是所有灯都是熄灭的。求最后亮着的灯的个数。思路首先,对于每个灯i,我们只需要确定它被翻转的次数x的奇偶性即可。当x为奇数时,最后i是亮着的。当x为偶数时,最后i是熄灭的。那么...
  • Lzedo
  • Lzedo
  • 2017-03-04 15:57
  • 93
    个人资料
    • 访问:12983次
    • 积分:504
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:11篇
    • 译文:0篇
    • 评论:2条