[Algorithm-Mathematics] Bulb Switch

LeetCode 319. Bulb Switcher
At the beginning, n bulbs were turned off. In the first round, turn on all the bulbs; in the second round, turn off every two bulbs; in the third round, switch once every three bulbs (if it is turned on, turn it off, if it is turned off, turn it on); in the nth turn, only switch The last light bulb switch. Find how many lights remain on after n rounds.

Example:
Input: 3
Output: 1
Explanation: State 0 means the bulb is off, 1 means on.
Initially, the bulb state [0 0 0]
after the first round, the bulb state [1 1 1]
after the second round, the bulb state [1 0 1]
after the third round, the bulb state [1 0 0]

public class BulbSwitcher {
    
    
	//n个灯泡开关n轮,最后满足开启的灯泡个数满足完全平方数
    public static int bulbSwitcher(int n) {
    
    
        if (n < 2) {
    
    
            return n;
        }
        return (int) Math.sqrt(n);
    }
}

Guess you like

Origin blog.csdn.net/runewbie/article/details/107053399