Skip to content

Commit 7ff632d

Browse files
author
kwu130
committed
feat(src): add 401-readBinaryWatch
1 parent 871aa29 commit 7ff632d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎src/401-readBinaryWatch.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by 1412 on 2026/4/11.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "common.h"
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
vector<string> readBinaryWatch(int turnedOn) {
13+
vector<string> res;
14+
for (int h = 0; h < 12; h++) {
15+
for (int m = 0; m < 60; m++) {
16+
if (__builtin_popcount(h) + __builtin_popcount(m) == turnedOn) {
17+
res.push_back(to_string(h) + ":" + (m < 10 ? "0" : "") + to_string(m));
18+
}
19+
}
20+
}
21+
return res;
22+
}
23+
};
24+
25+
void test() {
26+
Solution s;
27+
vector<string> expected = {"0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", "8:00"};
28+
vector<string> res = s.readBinaryWatch(1);
29+
assert(compareHelper::equalIgnoreOrder(res, expected));
30+
31+
expected = {};
32+
res = s.readBinaryWatch(9);
33+
assert(compareHelper::equalIgnoreOrder(res, expected));
34+
}
35+
36+
int main() {
37+
test();
38+
return 0;
39+
}

0 commit comments

Comments
 (0)