401. Binary Watch

本文介绍了一种特殊的手表——二进制手表的工作原理。这种手表使用4个LED灯显示小时(0-11),6个LED灯显示分钟(0-59)。每个LED灯代表一个二进制位,从右到左依次表示最低有效位。文章通过示例解释了如何读取二进制手表的时间,并提供了解决方案,即给定LED灯的数量,返回所有可能的时间表示。两种方法被提出:枚举法和深度优先搜索(DFS)。枚举法遍历所有可能的小时和分钟组合,检查它们的二进制位数是否与给定数量匹配;而DFS则通过递归方式生成所有可能的二进制位组合。

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:

  • The order of output does not matter.
  • The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
  • 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".

方法一:枚举法,枚举小时和分钟的二进制位数,和是否等于num

class Solution {
public:
    int countbit(int n){
        int cnt=0;
        while(n){
            if(n%2){
                cnt++;
            }
            n=n/2;
        }
        return cnt;
    }
    
    vector<string> readBinaryWatch(int num) {
        int h,m;
        vector<string>ans;
        string s1,s2;
        for(h=0;h<12;h++){
            for(m=0;m<60;m++){
                if((countbit(h)+countbit(m))==num){
                    s1=to_string(h);
                    s2=to_string(m);
                    if(m<10){
                        s2='0'+s2;
                    }
                    ans.push_back(s1+":"+s2);
                }
            }
        }
        return ans;
    }
};

方法二:dfs

思路参考这里

class Solution {
public:
    vector<string>ans;
    int timetable[10];
    
    vector<string> readBinaryWatch(int num) {
        dfs(0,0,num);
        return ans;
    }
    
    void dfs(int index,int k,int num){
        if(k==num){
            change();
            return ;//.....
        }
        if(index==10){//...
            return;
        }
        timetable[index]=1;
        dfs(index+1,k+1,num);
        timetable[index]=0;
        dfs(index+1,k,num);
    }
    
    void change(){
        int i,h=0,m=0;
        for(i=0;i<4;i++){
            if(timetable[i]==1){
                h=h+(int)pow(2,i);
            }
        }
        for(i=4;i<10;i++){
            if(timetable[i]==1){
                m=m+(int)pow(2,i-4);
            }
        }
        if(h<=11&&m<=59){
            string hour=to_string(h);
            string minute=to_string(m);
            if(m<10){
                minute="0"+minute;
            }
            ans.push_back(hour+":"+minute);
        }
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值