LeetCode Notes 011

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 ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

1
2
3
4
5
6
7
8
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.

1
2
3
4
5
6
7
8
9
10
/**
* @param {number} n
* @return {number}
*/

// 打表
var bulbSwitch = function(n) {
var ans = -1 + Math.sqrt(1 + n);
return Math.ceil(ans);
};

Bull and Cows

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

Example:

1
2
Secret number:  "1807"
Friend's guess: "7810"

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".

Please note that both secret number and friend’s guess may contain duplicate digits, for example:

1
2
Secret number:  "1123"
Friend's guess: "0111"

In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function(secret, guess) {
var a = secret.split('');
var b = guess.split('');
var len = a.length;

var bullNum = 0;
var cowBum = 0;

var hash = {};

for (var i = len; i--; ) {
if (a[i] === b[i]) {
a.splice(i, 1);
b.splice(i, 1);
bullNum++;
} else {
if (!hash[a[i]])
hash[a[i]] = 1;
else
hash[a[i]]++;
}
}


for (var i = 0; i < b.length; i++) {
var item = b[i];
if (hash[item]) {
cowBum++;
hash[item]--;
}
}

return bullNum + 'A' + cowBum + 'B';
};

Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

1
2
3
4
5
6
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.

1. 1 step + 1 step
2. 2 steps

Example 2:

1
2
3
4
5
6
7
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.

1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

1
2
3
4
5
6
7
8
9
10
11
12
/**
* @param {number} n
* @return {number}
*/

var climbStairs = function(n) {
var a = [];
a[0] = 1, a[1] = 1;
for(var i = 2; i <= n; i++)
a[i] = a[i - 1] + a[i - 2];
return a[n];
};
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/16/LeetCode-Notes-011/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏