leet-code/src/main/java/leetcode/editor/cn/BulbSwitcher.java
huangge1199@hotmail.com eda3db45a5 319:灯泡开关
2021-05-07 22:28:26 +08:00

75 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//初始时有 n 个灯泡处于关闭状态。
//
// 对某个灯泡切换开关意味着:如果灯泡状态为关闭,那该灯泡就会被开启;而灯泡状态为开启,那该灯泡就会被关闭。
//
// 第 1 轮,每个灯泡切换一次开关。即,打开所有的灯泡。
//
// 第 2 轮,每两个灯泡切换一次开关。 即,每两个灯泡关闭一个。
//
// 第 3 轮,每三个灯泡切换一次开关。
//
// 第 i 轮,每 i 个灯泡切换一次开关。 而第 n 轮,你只切换最后一个灯泡的开关。
//
// 找出 n 轮后有多少个亮着的灯泡。
//
//
//
// 示例 1
//
//
//
//
//输入n = 3
//输出1
//解释:
//初始时, 灯泡状态 [关闭, 关闭, 关闭].
//第一轮后, 灯泡状态 [开启, 开启, 开启].
//第二轮后, 灯泡状态 [开启, 关闭, 开启].
//第三轮后, 灯泡状态 [开启, 关闭, 关闭].
//
//你应该返回 1因为只有一个灯泡还亮着。
//
//
// 示例 2
//
//
//输入n = 0
//输出0
//
//
// 示例 3
//
//
//输入n = 1
//输出1
//
//
//
//
// 提示:
//
//
// 0 <= n <= 109
//
// Related Topics 脑筋急转弯 数学
// 👍 172 👎 0
package leetcode.editor.cn;
//319:灯泡开关
public class BulbSwitcher {
public static void main(String[] args) {
//测试代码
Solution solution = new BulbSwitcher().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int bulbSwitch(int n) {
return (int) Math.sqrt(n);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}