其实这个题可以枚举。如果想速度最快的话打表就好了,手动或者写个程序把10种情况都算出来。
考虑到循环次数也没多少,我写的算法是把每时每分都列出来看看加起来是不是给的值。其中判断整数的二进制表达里有几个1的算法在之前的博文里面介绍过。
class Solution {
public:
vector<string> readBinaryWatch(int num) {
vector<string> ans;
for (int i = 0; i < 12; i++)
for (int j = 0; j < 60; j++)
{
int tmpi = i;
int tmpj = j;
int hour = 0;
int min = 0;
while (tmpi)
{
tmpi = tmpi & (tmpi - 1);
hour++;
}
while (tmpj)
{
tmpj = tmpj & (tmpj - 1);
min++;
}
if (min + hour == num)
{
stringstream ss;
stringstream ss1;
std::string result;
ss << i;
ss >> result;
ss1 << j;
if (j < 10) result = result + ":0" + ss1.str();
else result = result + ":" + ss1.str();
ans.push_back(result);
}
}
return ans;
}
};
简洁写法:
vector<string> readBinaryWatch(int num) {
vector<string> rs;
for (int h = 0; h < 12; h++)
for (int m = 0; m < 60; m++)
if (bitset<10>(h << 6 | m).count() == num)
rs.emplace_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
return rs;
}
bitset类的相关知识
再贴一个花式打表的代码:
class Solution {
public:
vector<string> readBinaryWatch(int num) {
vector<vector<int>> hours{{0},{1,2,4,8},{3,5,9,6,10},{7,11}};
vector<vector<int>> minutes{{0},{1,2,4,8,16,32},{3,5,9,17,33,6,10,18,34,12,20,36,24,40,48},{7,11,19,35,13,21,37,25,41,49,14,22,38,26,42,50,28,44,52,56},{15,23,39,27,43,51,29,45,53,57,30,46,54,58},{31,47,55,59}};
vector<string> res;
for (int k = 0; k <= num; ++k) {
int t = num - k;
if (k > 3 || t > 5) continue;
for (int i = 0; i < hours[k].size(); ++i) {
for (int j = 0; j < minutes[t].size(); ++j) {
string str = minutes[t][j] < 10 ? "0" + to_string(minutes[t][j]) : to_string(minutes[t][j]);
res.push_back(to_string(hours[k][i]) + ":" + str);
}
}
}
return res;
}
};