[LeetCode]401. Binary Watch(二进制手表)

401. Binary Watch

原题地址
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
原题图片
Each LED represents a zero or one, with the least significant bit on the right.

For example, the above binary watch reads “3:25”.

Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

Example:

Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

Note:

  1. The order of output does not matter.输出顺序不影响
  2. The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
  3. The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.

题目大意:

二进制手表,上面一排代表小时,下面一排代表分钟
给一个整数代表LED灯亮的个数,返回一个字符串数组,显示所有可能的时间,时间格式注意Note的2,3

我的思路:

  • 首先先明白二进制手表的含义,把1,2,4,8转化为四位的二进制就是0001, 0010, 0100,1000, 9点时亮1和8,是1001。分钟数也是同理。
  • 其次表示小时的数值只有0-11,表示分钟的数值只有0-59。先分别对小时跟分钟的数值进行预处理,按照包含而二进制中包含1的个数分开保存小时数值的字符串跟分钟数值的字符串。
  • 以小时为例,小时使用4个位表示,最少包含0个1,最多包含3个1,所以用vector《vector《string》》 hours(4)建立一个包含4个vector的vector数组,hours[0]保存着0个1的数值对应的字符串”0”,hours[1]保存着有1个1的数值对应的字符串,即”1”,”2”,”4”,”8”,hours[2]保存着”3”,”5”,”6”,”9”,”10”,hours[3]保存着”7”,”11”。 定义numOfRow()处理这种情况
  • 读入num是LED灯亮的总个数,即小时和分钟的二进制中1的总个数
  • 小时表示亮灯个数h范围是0~3,分钟表示亮灯个数m范围是0~5,同时m = num -h
  • 定义一个字符串数组times,从h=0开始,在满足m=num -h<6和h<4的情况下,将所有可能的情况拼接成字符串,加入到times中,最后返回times

代码如下:

#include <iostream>
#include <vector>
#include <string>

using namespace std;
class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> times;
        if(num<0 || num>8)
            return times;
        vector<vector <int>> hours(4);
        vector<vector <int>> minutes(6);
        for(int i=0; i<12; i++){
            hours[numOfRow(i)].push_back(i);
        }
        for(int j=0; j<60; j++){
            minutes[numOfRow(j)].push_back(j);
        }
        for(int h=0; h<4&&h<=num; h++){
            if(num-h<6){
                for(int H: hours[h]){
                    for(int M: minutes[num-h]){
                        string time = M<10 ? ":0" : ":";
                        times.push_back(to_string(H) + time + to_string(M));
                    }
                }
            }
        }
        return times;
    }
    int numOfRow(int num){
        int Count = 0;
        while(num != 0){
            if(num%2 == 1)
                Count++;
            num /= 2;
        }
        return Count;
    }
};
int main()
{
    Solution a;
    int num = 0;
    cin >> num;
    vector<string> times = a.readBinaryWatch(num);
    for(int t=0; t<times.size(); t++)
        cout << times[t] << endl;
    return 0;
}

思路2:

  • 看到discuss有用bitset的,专门跑去学习一下bitset用法
  • 附上链接一链接二
  • 挺方便的,效率也比我暴力解出来的高

    代码如下:

#include <bitset>

vector<string> readBinaryWatch1(int num) {//bitset STL模板
        vector<string> times;
        for (int i = 0; i < 12; i++) {
            bitset<4> h(i);//4位的二进制数
            for (int j = 0; j < 60; j++) {
                bitset<6> m(j);//6位的二进制数
                if (h.count() + m.count() == num)//h.count()函数判断h中1的个数
                    times.push_back(to_string(i) + (j < 10? ":0": ":") + to_string(j));
            }
        }
        return times;
    }

另外,看到还有用动态规划的,附上链接,以供参考!

  1. 链接一
  2. 链接二
  3. 链接三
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值