LeetCode刷题笔记(Binary Watch)

继续刷,下面这道虽然被打上了easy的标签,但是需要点逆向思维才好解,下面就和大家分享一下经验吧!

题目如下:

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".

题意分析:

给定一个代表当前亮了的LED灯个数,请返回二进制表能表示的所有时间。其中由图中可以看出,二进制表上面四个LED灯以二进制形式表示小时,下面六个LED灯以二进制形式表示分钟。

注:

① 输出结果的顺序不重要;

② 输出结果中,小时位必须合法,如1:00合法,01:00不合法;

③ 输出结果中,分钟位必须合法,如10:02合法,10:2不合法;

 

方法一(递归法)

本问题可以看做给定10个LED灯,需要亮n个,求多少种亮法,其中每种亮法都对应着一个时间组合。其实本质还是解决从n个数中有多少种取k个数的组合问题,于是可以参考“https://blog.csdn.net/Vensmallzeng/article/details/96478481”中方法一的思路。本题一共需要取num个数,先在小时数组中取i个元素并计算它们的和,同时在分钟数组中取num-i个元素并计算它们的和,如果两个部分的和分别符合小时和分钟的范围,那么将该时间组合存入res中,最后返回res即可。

解题代码如下:

class Solution{
public:
    vector<string> readBinaryWatch(int num){
        vector<string> res;
        vector<int> hour{8, 4, 2, 1}, minute{32, 16, 8, 4, 2, 1};
        for(int i = 0; i <= num; i++){
            vector<int> hours = combination(hour, i);
            vector<int> minutes = combination(minute, num - i);
            for (int hour : hours) {
                if (hour>11)  continue;
                for (int minute : minutes) {
                    if (minute>59)  continue;
                    res.push_back(to_string(hour) + (minute<10 ? ":0" : ":") + to_string(minute));
                }
            }
        }
        return res;
    }

    vector<int> combination(vector<int>& nums, int k){
        vector<int> res;
        Findcombination(nums, k, 0, 0, res);
        return res;
    }

    void Findcombination(vector<int>& nums, int k, int start, int temp, vector<int>& res){
        if(k==0)
        {
            res.push_back(temp);
            return;
        }
        for (int i = start; i < nums.size(); ++i) {
            Findcombination(nums, k-1, i+1, temp + nums[i], res);
        }
    }

};

提交后的结果如下:

 

方法二(非递归法)

本方法需要用到可以将任意进制数转为二进制的bitset类,同时采用count函数去统计二进制数为1的位数。首先,通过两层for循环,外层从0遍历到11(小时),内层从0遍历到59(分钟),遍历所有可能的时间组合,然后把小时数左移6位加上分钟数并将所得结果通过bitset类转成二进制数,然后采用count函数统计位数为1的个数,也即亮了的LED灯个数。当遍历到统计个数与num相等时,将该时间组合存入res中,最后返回res即可。

解题代码如下:

class Solution{
public:
    vector<string> readBinaryWatch(int num){
        vector<string> res;
        for (int i = 0; i < 12; ++i) {
            for (int j = 0; j < 60; ++j) {
                if(bitset<10>((i<<6) + j).count() == num)
                      j<10? res.push_back(to_string(i)+":0"+to_string(j)):res.push_back(to_string(i)+":"+to_string(j));
                        
            }
        }
        return res;
    }
};

提交后的结果如下:


 

 

日积月累,与君共进,增增小结,未完待续。  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值