LeetCode 401. Binary Watch 题解(C++)

questasim的傻瓜式安装与仿真教程 一、安装 1、下载软件 链接:https://pan.baidu.com/s/1S05AnO_w-wy6RsAj_kdUXg 提取码:p2nt 2、根据电脑类型是32位还是64位,解压不同安装包。 解压得: 2、运行.exe文件,出现: 问题及解决办法: 这是杀毒软件造成的,因此在安装过程中关闭杀毒软件即可。关闭之后,再次运行.exe文件。 3、点击下一步,进入选择安装位置界面,如下:选择好安装位置后点击下一步。 4、点击同意 5、接下来等待安装结束即可,在安装过程中,会跳出来弹框,询问是否要添 阅读详情

LeetCode 401. Binary Watch 题解(C++)


题目描述

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

举例

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

补充

  • 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”.

思路

思路1
  • 对小时h和分钟m进行遍历,总共有12*60种可能(分别为12小时和60分钟),之后将小时h右移6位(右边6位为分钟m的二进制)转换成题目要求的10个二进制,并调用bitset的count计算二进制中1的个数,若1的个数等于num,则该时间满足题目要求,这里需要注意时间的格式。
思路2
  • 使用回溯法,递归的出口为num=0;对10个灯做深度优先搜索,注意需要剪枝,即小时要小于12,分钟要小于60,参见代码2。

代码

代码1
class Solution
{
public:
    vector<string> readBinaryWatch(int num)
    {
        vector<string> res;
        for (int h = 0; h < 12; ++h)
        {
            for (int m = 0; m < 60; ++m)
            {
                if (bitset<10>(h << 6 | m).count() == num)
                {
                    res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
                }
            }
        }
        return res;
    }
};
代码2
class Solution 
{
    vector<int> hour = {1, 2, 4, 8}, minute = {1, 2, 4, 8, 16, 32};
public:
    vector<string> readBinaryWatch(int num) 
    {
        vector<string> res;
        helper(res, make_pair(0, 0), num, 0);
        return res;
    }

    void helper(vector<string>& res, pair<int, int> time, int num, int start_point)
    {
        if (num == 0)
        {
            if (time.second < 10)
                res.push_back(to_string(time.first) + ":0" + to_string(time.second));
            else
                res.push_back(to_string(time.first) + ":" + to_string(time.second));
            return;
        }
        for (int i = start_point; i < hour.size() + minute.size(); i ++)
        {
            if (i < hour.size())
            {
                time.first += hour[i];
                if (time.first < 12)    // "hour" should be less than 12.
                    helper(res, time, num - 1, i + 1);
                time.first -= hour[i];
            }
            else
            {
                time.second += minute[i - hour.size()];
                if (time.second < 60)    // "minute" should be less than 60.
                    helper(res, time, num - 1, i + 1);
                time.second -= minute[i - hour.size()];
            }
        }
    }
};
Python 爬虫实战:豆瓣电影海报批量获取(Top250 榜单 + 可视化展示) 在信息爆炸的时代,数据已成为决策的核心,而数据的获取是数据分析的第一步。本文将详细介绍如何利用 Python 爬虫获取豆瓣电影 Top250 榜单的海报,并对数据进行可视化展示,助你更好地了解电影市场的趋势和用户的喜好。 阅读详情

相关推荐

HUAWEI华为荣耀MagicBook MateBook B D E X 13 14 16 笔记本MateStation台式机原装Win10出厂Windows11系统

HUAWEI华为MateBookX原装出厂系统恢复原厂系统,型号:MACHC-WAE9LP、EUL-W29P、MACHR-W19L、MACHR-W29、MACHD-WFE9、MRGF-16、MACHR-W19BL、MACHD-WFE9Q、WT-W19、Mach-W19、Mach-W29、WT-W09、WT-W19、MACH-W29等。HUAWEI华为MateBookE原装出厂系统恢复原厂系统,型号:PAK-AL09、GK-W7X、GK-G58、GK-W76、DRC-W76等。

QQ17855069的博客 17万+

LeetCode:401(Python)—— 二进制手表(简单)

概述:二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。

m0_61661179的博客 3万+

微信DAT图片恢复全攻略:从文件路径到批量解密(附最新工具下载)

本文详细介绍了微信DAT图片恢复的全过程,从文件路径定位到批量解密操作,帮助用户高效恢复加密存储的图片。文章涵盖新旧版本微信的存储结构差异,推荐使用专业解密工具进行批量转换,并提供了提升恢复成功率的实用技巧和常见问题解决方案。

weixin_29325007的博客 455

【LeetCode每日一题】——401.二进制手表

二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。 例如,下面的二进制手表读取 "4:51" 。 给你一个整数 turnedOn ,表示当前亮着的 LED 的数量,返回二进制手表可以表示的所有可能时间。你可以 按任意顺序 返回答案。 小时不会以零开头: 例如,"01:00" 是无效的时间,正确的写法应该是 "1:00" 。

IronmanJay 1268

从GitHub到海浪模拟:手把手教你配置WAVEWATCHⅢ 6.07.0的完整开发与测试环境

本文详细指导如何在Ubuntu系统中配置WAVEWATCHⅢ 6.07.0的开发与测试环境,涵盖虚拟机设置、依赖安装、源码编译及案例验证的全流程。通过步骤化教程,帮助科研人员快速搭建高效的海浪模拟平台,提升海洋数值模拟的研究效率。

weixin_42568801的博客 324

Unity资源逆向解析:从APK到AssetStudio的完整实战

本文详细介绍了Unity资源逆向解析的全流程,从APK解包到使用AssetStudio深度解析Unity游戏资源。通过实战案例演示如何提取《植物大战僵尸》等游戏的素材,包括贴图、音频和3D模型,帮助开发者掌握Unity游戏逆向工程的核心技术。

weixin_30664051的博客 475

Leetcode401. 二进制手表

Leetcode401. 二进制手表

ProgramNovice的博客 137

智能家居系统设计:基于MATLAB的智能家居系统设计和仿真,包括智能家居控制、家庭能源管理和家庭安全监控

基于 MATLAB 的智能家居系统设计方法可以有效地解决各类智能家居问题。

走向CTO的路上... 1775

LeetCode 401. 二进制手表(二进制枚举)

401. 二进制手表 二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。 每个 LED 代表一个 0 或 1,最低位在右侧。 例如,上面的二进制手表读取 “3:25”。 给定一个非负整数 n 代表当前 LED 亮着的数量,返回所有可能的时间。 示例 : 输入: n = 1 返回: [“1:00”, “2:00”, “4:00”, “8:00”, “0:01”, “0:02”, “0:04”, “0:08”, “0:16”, “0:32”]

LittleTime_cc的博客 436

成对使用离轴抛物面反射镜的设计

成对使用离轴抛物面反射镜的设计

weixin_72050316的博客 638

如何实现一个用STM32F103C8T6作为主控芯片,加上AD8232、MAX30102 、MPU6050模块的用于运动中监测心率变异性??

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1213

(leetcode)401. 二进制手表2021/6/21

文章目录496. 下一个更大元素 I javascript 496. 下一个更大元素 I javascript 题目: 二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。 例如,下面的二进制手表读取 “3:25” 。 给你一个整数 turnedOn ,表示当前亮着的 LED 的数量,返回二进制手表可以表示的所有可能时间。你可以 按任意顺序 返回答案。 小时不会以零开头: 例如,“01:00” 是无效的时

FemaleHacker的博客 342

prplOS介绍

介绍prplOS是什么,特性、框架、编译等。

四儿家的小祖宗的博客 2130

LeetCode //C - 401. Binary Watch

【代码】LeetCode //C - 401. Binary Watch。

Made in Code 1246

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode 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 signif

rentiansky的专栏 351

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

401. Binary Watch

原题网址:https://leetcode.com/problems/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 repr

James Pan 492

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode-401. Binary Watch

Description 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 sig...

zy2317878的博客 254

leetcode_[python/java/javascript/C++]_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 signifi

Mosen 882

【LeetCode】401. Binary Watch

问题描述问题链接:https://leetcode.com/problems/binary-watch/#/descriptionA 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 LE

墨染百城 375

leetcode 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 bi...

djph26741的博客 102

LeetCode(401):二进制手表 Binary Watch(Java)

2021.1.11 LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 可以有两种思路: 1.遍历所有符合要求的时间,统计二进制中1的个数,把个数等于num的时间筛选出来。 2.回溯法遍历所有LED,记录符合条件的时间。 传送门:二进制手表 A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 L

NJU_ChopinXBP的博客 317

LeetCode C++ 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:

memcpy0的博客 434

leetcode 401. Binary Watch二进制手表

leetcode 401. Binary Watch二进制手表

Shauna_Wu的博客 991

LeetCode - 401. Binary Watch - 思路详解-C++

一个二进制手表,4个LED在顶部,表示小时从0~11。6个LED在底部,表示分钟 0 ~ 59 每个LED表示为0或者1,右边最低有效位。 给一个正整数,表示整个手表点亮的LED的个数。返回所有可能的时间。

SJWL2012的专栏 689

leetcode 401. Binary Watch 一个十分经典的DFS深度优先遍历的做法 + C++的bitset的很不错使用案例

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

JackZhangNJU的专栏 582

leetcode:401. 二进制手表

此外,分钟和小时所拥有的灯数也是不一样的,因此for循环也需要布尔值来设定搜索范围。个灯,可得时钟显示剩余的可用灯数为。为num,分钟的显示用了。

OceanStar的博客 942
上一篇: LeetCode 268. Missing Number 题解(C++)
下一篇: LeetCode 94. Binary Tree Inorder Traversal 题解(C++)
prayyojay
博客等级 码龄10年 4粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值