Skip to content

Commit d544577

Browse files
committed
Add solution #401
1 parent 0d8cbf3 commit d544577

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
398|[Random Pick Index](./0398-random-pick-index.js)|Medium|
320320
399|[Evaluate Division](./0399-evaluate-division.js)|Medium|
321321
400|[Nth Digit](./0400-nth-digit.js)|Medium|
322+
401|[Binary Watch](./0401-binary-watch.js)|Easy|
322323
404|[Sum of Left Leaves](./0404-sum-of-left-leaves.js)|Easy|
323324
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
324325
407|[Trapping Rain Water II](./0407-trapping-rain-water-ii.js)|Hard|

solutions/0401-binary-watch.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 401. Binary Watch
3+
* https://leetcode.com/problems/binary-watch/
4+
* Difficulty: Easy
5+
*
6+
* A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom
7+
* to represent the minutes (0-59). Each LED represents a zero or one, with the least significant
8+
* bit on the right.
9+
* - For example, the below binary watch reads "4:51".
10+
* Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring
11+
* the PM), return all possible times the watch could represent. You may return the answer in
12+
* any order.
13+
* The hour must not contain a leading zero.
14+
* - For example, "01:00" is not valid. It should be "1:00".
15+
* The minute must consist of two digits and may contain a leading zero.
16+
* - For example, "10:2" is not valid. It should be "10:02".
17+
*/
18+
19+
/**
20+
* @param {number} turnedOn
21+
* @return {string[]}
22+
*/
23+
var readBinaryWatch = function(turnedOn) {
24+
const result = [];
25+
26+
for (let h = 0; h < 12; h++) {
27+
for (let m = 0; m < 60; m++) {
28+
if (h.toString(2).split('1').length - 1 + m.toString(2).split('1').length - 1 === turnedOn) {
29+
result.push(`${h}:${m < 10 ? '0' + m : m}`);
30+
}
31+
}
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)