Skip to content

Commit 7c639f5

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 10 MB (20.35%)
1 parent 24d1c45 commit 7c639f5

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent&nbsp;the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.</p>
2+
3+
<ul>
4+
<li>For example, the below binary watch reads <code>&quot;4:51&quot;</code>.</li>
5+
</ul>
6+
7+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg" style="width: 500px; height: 500px;" /></p>
8+
9+
<p>Given an integer <code>turnedOn</code> which represents the number of LEDs that are currently on (ignoring the PM), return <em>all possible times the watch could represent</em>. You may return the answer in <strong>any order</strong>.</p>
10+
11+
<p>The hour must not contain a leading zero.</p>
12+
13+
<ul>
14+
<li>For example, <code>&quot;01:00&quot;</code> is not valid. It should be <code>&quot;1:00&quot;</code>.</li>
15+
</ul>
16+
17+
<p>The minute must&nbsp;consist of two digits and may contain a leading zero.</p>
18+
19+
<ul>
20+
<li>For example, <code>&quot;10:2&quot;</code> is not valid. It should be <code>&quot;10:02&quot;</code>.</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
<pre><strong>Input:</strong> turnedOn = 1
26+
<strong>Output:</strong> ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
27+
</pre><p><strong class="example">Example 2:</strong></p>
28+
<pre><strong>Input:</strong> turnedOn = 9
29+
<strong>Output:</strong> []
30+
</pre>
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>0 &lt;= turnedOn &lt;= 10</code></li>
36+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
void getTime(int binary, vector<string>& res) {
3+
int hour = (binary & 0b1111000000) >> 6;
4+
int minute = binary & 0b111111;
5+
if (hour > 11 || minute > 59) return;
6+
string time = to_string(hour) + (minute < 10 ? ":0" : ":") + to_string(minute);
7+
res.push_back(time);
8+
}
9+
10+
public:
11+
vector<string> readBinaryWatch(int turnedOn) {
12+
vector<string> res;
13+
for (int i=0; i<1024; i++) {
14+
if (__builtin_popcount(i) == turnedOn) getTime(i, res);
15+
}
16+
return res;
17+
}
18+
};

0 commit comments

Comments
 (0)