Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 401. Binary Watch
題目:

解答:
class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
auto bits = [](int v) {
int ans = 0;
for (int i = 0; i < 6; ++i) {
ans += (v & 1);
v >>= 1;
}
return ans;
};
vector<string> ans;
for (int h = 0; h < 12; ++h) {
for (int m = 0; m < 60; ++m) {
if (bits(h) + bits(m) == turnedOn) {
string t = to_string(h) + ":";
if (m < 10) {
t += "0";
}
t += to_string(m);
ans.push_back(t);
}
}
}
return ans;
}
};