栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

二进制手表,1的个数可能的时间

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

二进制手表,1的个数可能的时间

1、描述

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

例如,下面的二进制手表读取 “3:25” 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-watch
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、关键字

二进制手表,根据1的个数,求可能的时间点,

3、思路

暴力两成循环,外层是小时,内层是分钟,
搞一个统计1个数的函数,

int count1(int n){ // 求这个n,中1的个数,
        int res = 0;
        while(n){
            n = n & (n - 1);
            res++;
        }
        return res;
    }

直接暴力统计,

4、notes

就暴力统计

5复杂度 6、code
class Solution {
public:
    vector readBinaryWatch(int turnedOn) {
        vectorres;
        for(int i = 0; i < 12; i++){
            for(int j = 0; j < 60; j++){
                if(count1(i) + count1(j) == turnedOn){
                    res.push_back(   to_string(i) + ":"+ 
                    (j < 10 ? "0"+to_string(j) : to_string(j) )      ) ;
                }
            }
        }
        return res;

    }
    int count1(int n){ // 求这个n,中1的个数,
        int res = 0;
        while(n){
            n = n & (n - 1);
            res++;
        }
        return res;
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号