【LeetCode从零单刷】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
nth round, you only toggle the last bulb.Find how many bulbs are on after
n rounds.

Example:

Given n = 3. 

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.

解答:

我们知道,每当灯泡会改变状态,也就是 toggle 时,是因为它出现在了某个数的整数倍上。

对于第1个灯泡:1*1,会改变1次状态,即 off -》on

对于第2个灯泡:1*2,2*1,会改变2次状态,即 off -》on -》off

对于第3个灯泡:1*3,3*1,会改变2次状态,即 off -》on -》off

对于第4个灯泡:1*4,2*2,4*1,会改变3次状态,即 off -》on -》off -》on

……

会发现,每当我找到一个数的整数倍,总会找到对称的一个整数倍,例如 1*2,就肯定会有一个 2*1。唯一的例外出现在平方数上,例如 4 = 2*2,只有一次整数倍。

每次作为偶数次整数倍,最终的灯泡都会还原为 off;只有作为奇数次整数倍,最终的灯泡都会 on。

也就是说,最终亮的灯泡数目由小于其的最大平方数确定。代码非常简单:

class Solution {
public:
    int bulbSwitch(int n) {
        return (int)sqrt(n);
    }
};
时间: 2024-10-12 12:56:03

【LeetCode从零单刷】Bulb Switcher的相关文章

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given a binary tree, find its maximum depth.  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高

【LeetCode从零单刷】Same Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解答: 验证两棵树是否相等.使

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

【LeetCode从零单刷】Excel Sheet Column Number

题目: Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 解答: 怎么说,类似26进制数一样.乍看之下挺简单.于是写下: class Solution { public: int titleToNumber(stri

【LeetCode从零单刷】Populating Next Right Pointers in Each Node

题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially

【LeetCode从零单刷】Ugly Number

题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 i

【LeetCode从零单刷】Find the Duplicate Number

题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modif

【LeetCode从零单刷】Majority Element

题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 解答: 寻找主元素.主元素有几个性质: 主元素,