Bulb Switcher (leetcode java)

问题描述:

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.

问题分析:

/**
 *
 * 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.
 *
 * 一、从给定的题目中找到规律:就是,当 i 为  1 到 n之间的任何数时,翻转 k = ri 位置上灯泡的状态。
 * 求n轮之后,还有几盏灯是开着的?
 * 二、最直观的方法是穷举法:一轮轮的遍历,但是,当n很大时,时间复杂度太大。
 * 三、找规律:
 * 若起初有5盏灯,经过5轮翻转后,最终只有第1盏和第4盏是亮着的,不妨分析下,这里面是否有什么规律?
 * 起始:5盏灯全部为 off
 * 1 = (1,1)                                       1次翻转,最终on
 * 2 = (1,2),(2,1)                      2次翻转,最终off
 * 3 = (1,3),(3,1)                      2次翻转,最终off
 * 4 = (1,4),(2,2),(4,1)    3次翻转,最终on
 * 5 = (1,5),(5,1)                      2次翻转,最终off
 *
 * 可以看出,最终为on的灯所在位置都是平方数
 * @author admin
 *
 */

代码:1到n之间 平方数的个数 为: sqrt(n) 向下取整。

//返回n轮之后,有多少盏灯是开着的
    public static int bulbSwitch(int n){
        if(n <= 0)
            return 0;
        int num = (int)Math.sqrt(n); //求n的平方根,double强制转换为int类型,即为向下取整。

        System.out.println(num);

        return num;
    }
时间: 2024-11-05 02:50:15

Bulb Switcher (leetcode java)的相关文章

[LeetCode][JavaScript]Bulb Switcher

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

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: