401. Binary Watch的C++解法

其实这个题可以枚举。如果想速度最快的话打表就好了,手动或者写个程序把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;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值