基础数学(三)位运算 JZ 15.位1的个数

news2024/11/17 14:17:57

正在刷DFS相关题的时候突然间,给我蹦出来这样一个回溯题:

401. 二进制手表

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

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

 然后被这个人炫了一手这!!!

 评论区神仙:暴力枚举

func readBinaryWatch(_ num: Int) -> [String] {
        switch num {
        case 0:
            return ["0:00"]
        case 1:
            return ["0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", "8:00"]
        case 2:
            return ["0:03", "0:05", "0:06", "0:09", "0:10", "0:12", "0:17", "0:18", "0:20", "0:24", "0:33", "0:34", "0:36", "0:40", "0:48", "1:01", "1:02", "1:04", "1:08", "1:16", "1:32", "2:01", "2:02", "2:04", "2:08", "2:16", "2:32", "4:01", "4:02", "4:04", "4:08", "4:16", "4:32", "8:01", "8:02", "8:04", "8:08", "8:16", "8:32", "3:00", "5:00", "6:00", "9:00", "10:00"]
        case 3:
            return ["0:07", "0:11", "0:13", "0:14", "0:19", "0:21", "0:22", "0:25", "0:26", "0:28", "0:35", "0:37", "0:38", "0:41", "0:42", "0:44", "0:49", "0:50", "0:52", "0:56", "1:03", "1:05", "1:06", "1:09", "1:10", "1:12", "1:17", "1:18", "1:20", "1:24", "1:33", "1:34", "1:36", "1:40", "1:48", "2:03", "2:05", "2:06", "2:09", "2:10", "2:12", "2:17", "2:18", "2:20", "2:24", "2:33", "2:34", "2:36", "2:40", "2:48", "4:03", "4:05", "4:06", "4:09", "4:10", "4:12", "4:17", "4:18", "4:20", "4:24", "4:33", "4:34", "4:36", "4:40", "4:48", "8:03", "8:05", "8:06", "8:09", "8:10", "8:12", "8:17", "8:18", "8:20", "8:24", "8:33", "8:34", "8:36", "8:40", "8:48", "3:01", "3:02", "3:04", "3:08", "3:16", "3:32", "5:01", "5:02", "5:04", "5:08", "5:16", "5:32", "6:01", "6:02", "6:04", "6:08", "6:16", "6:32", "9:01", "9:02", "9:04", "9:08", "9:16", "9:32", "10:01", "10:02", "10:04", "10:08", "10:16", "10:32", "7:00", "11:00"]
        case 4:
            return ["0:15", "0:23", "0:27", "0:29", "0:30", "0:39", "0:43", "0:45", "0:46", "0:51", "0:53", "0:54", "0:57", "0:58", "1:07", "1:11", "1:13", "1:14", "1:19", "1:21", "1:22", "1:25", "1:26", "1:28", "1:35", "1:37", "1:38", "1:41", "1:42", "1:44", "1:49", "1:50", "1:52", "1:56", "2:07", "2:11", "2:13", "2:14", "2:19", "2:21", "2:22", "2:25", "2:26", "2:28", "2:35", "2:37", "2:38", "2:41", "2:42", "2:44", "2:49", "2:50", "2:52", "2:56", "4:07", "4:11", "4:13", "4:14", "4:19", "4:21", "4:22", "4:25", "4:26", "4:28", "4:35", "4:37", "4:38", "4:41", "4:42", "4:44", "4:49", "4:50", "4:52", "4:56", "8:07", "8:11", "8:13", "8:14", "8:19", "8:21", "8:22", "8:25", "8:26", "8:28", "8:35", "8:37", "8:38", "8:41", "8:42", "8:44", "8:49", "8:50", "8:52", "8:56", "3:03", "3:05", "3:06", "3:09", "3:10", "3:12", "3:17", "3:18", "3:20", "3:24", "3:33", "3:34", "3:36", "3:40", "3:48", "5:03", "5:05", "5:06", "5:09", "5:10", "5:12", "5:17", "5:18", "5:20", "5:24", "5:33", "5:34", "5:36", "5:40", "5:48", "6:03", "6:05", "6:06", "6:09", "6:10", "6:12", "6:17", "6:18", "6:20", "6:24", "6:33", "6:34", "6:36", "6:40", "6:48", "9:03", "9:05", "9:06", "9:09", "9:10", "9:12", "9:17", "9:18", "9:20", "9:24", "9:33", "9:34", "9:36", "9:40", "9:48", "10:03", "10:05", "10:06", "10:09", "10:10", "10:12", "10:17", "10:18", "10:20", "10:24", "10:33", "10:34", "10:36", "10:40", "10:48", "7:01", "7:02", "7:04", "7:08", "7:16", "7:32", "11:01", "11:02", "11:04", "11:08", "11:16", "11:32"]
        case 5:
            return ["0:31", "0:47", "0:55", "0:59", "1:15", "1:23", "1:27", "1:29", "1:30", "1:39", "1:43", "1:45", "1:46", "1:51", "1:53", "1:54", "1:57", "1:58", "2:15", "2:23", "2:27", "2:29", "2:30", "2:39", "2:43", "2:45", "2:46", "2:51", "2:53", "2:54", "2:57", "2:58", "4:15", "4:23", "4:27", "4:29", "4:30", "4:39", "4:43", "4:45", "4:46", "4:51", "4:53", "4:54", "4:57", "4:58", "8:15", "8:23", "8:27", "8:29", "8:30", "8:39", "8:43", "8:45", "8:46", "8:51", "8:53", "8:54", "8:57", "8:58", "3:07", "3:11", "3:13", "3:14", "3:19", "3:21", "3:22", "3:25", "3:26", "3:28", "3:35", "3:37", "3:38", "3:41", "3:42", "3:44", "3:49", "3:50", "3:52", "3:56", "5:07", "5:11", "5:13", "5:14", "5:19", "5:21", "5:22", "5:25", "5:26", "5:28", "5:35", "5:37", "5:38", "5:41", "5:42", "5:44", "5:49", "5:50", "5:52", "5:56", "6:07", "6:11", "6:13", "6:14", "6:19", "6:21", "6:22", "6:25", "6:26", "6:28", "6:35", "6:37", "6:38", "6:41", "6:42", "6:44", "6:49", "6:50", "6:52", "6:56", "9:07", "9:11", "9:13", "9:14", "9:19", "9:21", "9:22", "9:25", "9:26", "9:28", "9:35", "9:37", "9:38", "9:41", "9:42", "9:44", "9:49", "9:50", "9:52", "9:56", "10:07", "10:11", "10:13", "10:14", "10:19", "10:21", "10:22", "10:25", "10:26", "10:28", "10:35", "10:37", "10:38", "10:41", "10:42", "10:44", "10:49", "10:50", "10:52", "10:56", "7:03", "7:05", "7:06", "7:09", "7:10", "7:12", "7:17", "7:18", "7:20", "7:24", "7:33", "7:34", "7:36", "7:40", "7:48", "11:03", "11:05", "11:06", "11:09", "11:10", "11:12", "11:17", "11:18", "11:20", "11:24", "11:33", "11:34", "11:36", "11:40", "11:48"]
        case 6:
            return ["1:31", "1:47", "1:55", "1:59", "2:31", "2:47", "2:55", "2:59", "4:31", "4:47", "4:55", "4:59", "8:31", "8:47", "8:55", "8:59", "3:15", "3:23", "3:27", "3:29", "3:30", "3:39", "3:43", "3:45", "3:46", "3:51", "3:53", "3:54", "3:57", "3:58", "5:15", "5:23", "5:27", "5:29", "5:30", "5:39", "5:43", "5:45", "5:46", "5:51", "5:53", "5:54", "5:57", "5:58", "6:15", "6:23", "6:27", "6:29", "6:30", "6:39", "6:43", "6:45", "6:46", "6:51", "6:53", "6:54", "6:57", "6:58", "9:15", "9:23", "9:27", "9:29", "9:30", "9:39", "9:43", "9:45", "9:46", "9:51", "9:53", "9:54", "9:57", "9:58", "10:15", "10:23", "10:27", "10:29", "10:30", "10:39", "10:43", "10:45", "10:46", "10:51", "10:53", "10:54", "10:57", "10:58", "7:07", "7:11", "7:13", "7:14", "7:19", "7:21", "7:22", "7:25", "7:26", "7:28", "7:35", "7:37", "7:38", "7:41", "7:42", "7:44", "7:49", "7:50", "7:52", "7:56", "11:07", "11:11", "11:13", "11:14", "11:19", "11:21", "11:22", "11:25", "11:26", "11:28", "11:35", "11:37", "11:38", "11:41", "11:42", "11:44", "11:49", "11:50", "11:52", "11:56"]
        case 7:
            return ["3:31", "3:47", "3:55", "3:59", "5:31", "5:47", "5:55", "5:59", "6:31", "6:47", "6:55", "6:59", "9:31", "9:47", "9:55", "9:59", "10:31", "10:47", "10:55", "10:59", "7:15", "7:23", "7:27", "7:29", "7:30", "7:39", "7:43", "7:45", "7:46", "7:51", "7:53", "7:54", "7:57", "7:58", "11:15", "11:23", "11:27", "11:29", "11:30", "11:39", "11:43", "11:45", "11:46", "11:51", "11:53", "11:54", "11:57", "11:58"]
        case 8:
            return ["7:31", "7:47", "7:55", "7:59", "11:31", "11:47", "11:55", "11:59"]
        default:
            return []
        }

 于是利用二进制求1的个数来解决这个问题

class Solution {
public:
    // 计算二进制中1的个数
    int count(int n)
    {
        int res = 0;
        while(n)
        {
            n = n&(n-1);
            res++;
        }
        return res;
    }
    vector<string> readBinaryWatch(int turnedOn) {
        vector<string> res;
        for(int i = 0; i < 12; i++)
        {
            for(int j = 0; j < 60; j++)
            {
                if(count(i)+count(j)==turnedOn)
                    res.push_back(to_string(i) +":"+ (j<10?"0":"")+to_string(j));
            }
        }
        return res;
    }
};

想起来自己对于位运算有一道技巧题一直想整理但却找不见,这次终于在评论区中见到了

191. 位1的个数

编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量)。

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res = 0;
        for(int i = 0; i < 32; i++)
        {
            // if((1&(n>>i)) == 1)
            //     res++;
            res += (1&(n>>i)); 右移i次与1相与
        }
        return res;
    }
}; 

     // 计算二进制中1的个数
    int count(int n)
    {
        int res = 0;
        while(n)
        {
            n = n&(n-1);
            res++;
        }
        return res;
    }  这个代码每次都能减少一个二进制中的1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/174489.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

maven 解决Cannot access alimaven以及Process terminated

maven 解决Cannot access alimaven以及Process terminated 目录maven 解决Cannot access alimaven以及Process terminated方案一&#xff1a;用idea打开settings.xml&#xff0c;更正红色报错方案二&#xff1a;将IDEA的Maven默认版本更换成你下载的maven文件夹方案三&#xff…

单片机堆栈知识总结

堆栈 在片内RAM中&#xff0c;常常要指定一个专门的区域来存放某些特别的数据 它遵循顺序存取和后进先出(LIFO/FILO)的原则&#xff0c;这个RAM区叫堆栈。 其实堆栈就是单片机中的一些存储单元&#xff0c;这些存储单元被指定保存一些特殊信息&#xff0c;比如地址&#xff0…

DFS(二)岛屿问题合集

目录 一、 463. 岛屿的周长 二、 130. 被围绕的区域 三、 200. 岛屿数量 四、695. 岛屿的最大面积 一、463. 岛屿的周长 给定一个 row x col 的二维网格地图 grid &#xff0c;其中&#xff1a;grid[i][j] 1 表示陆地&#xff0c; grid[i][j] 0 表示水域。 网格中的格子 …

Java设计模式-解释器模式、解释器模式什么回事,抽象语法树又是什么

继续整理记录这段时间来的收获&#xff0c;详细代码可在我的Gitee仓库SpringBoot克隆下载学习使用&#xff01; 6.12 解释器模式 6.12.1 概述 思维&#xff1a;翻译识别机器&#xff0c;如解析由数字、“”、“-”号构成的合法运算序列&#xff0c;若将数字和字符看作结点&a…

Lesson 4.1 逻辑回归模型构建与多分类学习方法

文章目录一、广义线性模型&#xff08;Generalized liner model&#xff09;的基本定义二、对数几率模型与逻辑回归1. 对数几率模型&#xff08;logit model&#xff09;2. 逻辑回归与 Sigmoid 函数3. Sigmoid 函数性质三、逻辑回归模型输出结果与模型可解释性四、多分类学习与…

CPU缓存一致性

CPU缓存一致性写直达写回缓存一致性总线嗅探MESI协议CPU Cache通常分为三级缓存&#xff0c;L1Cache&#xff0c;L2Cache,L3Cache&#xff0c;级别越低的离CPU越近&#xff0c;访问速度越快&#xff0c;但同时容量越小&#xff0c;价格越贵。在多核的CPU中&#xff0c;每个核都…

今天大年三十,新年快乐,我在这里给大家整理了一下除夕的习俗,来看看吧

今天是大年三十,阿玥在这里祝大家,一来风水,二来平安,阖家欢乐,四季平安,五福临门,六六大顺,七星高照,八方来财,十全十美,新年好! 名字:不晓得 学习:python,c 主页:木有 今天给大家整理一下大年三十的习俗等小知识,就不更python啦 目录 除夕要做的事情有什么…

Meta CTO:真正的全天候轻量化AR眼镜,可能要到2030年

去年Meta发布了售价高达1500美元的VST头显Quest Pro&#xff0c;该头显与Meta的Quest 2等产品在定价、技术路径上有很大不同&#xff0c;其搭载了眼球追踪、彩色VST等更高端的功能&#xff0c;而产品发布后&#xff0c;外界对其反馈也褒贬不一。作为Pro产品线首个产品&#xff…

Markdown基础总结

Markdown Tools TyporaVSCode Markdown Preview Enhanced扩展 有道云笔记… 上述工具都能很好地支持markdown书写 Markdown标题 1 使用 和 - 标记一级和二级标题 和 - 标记语法格式如下&#xff1a; 我展示的是一级标题我展示的是二级标题 ---效果如下: 2 使用 # 号标…

Java设计模式-访问者模式、访问者模式怎么使用,具体是怎么用

继续整理记录这段时间来的收获&#xff0c;详细代码可在我的Gitee仓库SpringBoot克隆下载学习使用&#xff01; 6.10 访问者模式 6.10.1 定义 封装一些作用域某种数据结构中的各元素的操作&#xff0c;可以在不改变此数据结构的前提下定义作用于这些元素的新操作 6.10.2 结…

2023年新年烟花代码(背景音乐完整版)

文章目录前言烟花效果展示使用教程查看源码HTML代码CSS代码JavaScript新年祝福前言 大家过年好&#xff01;新春佳节&#xff0c;在这个充满喜悦的日子里&#xff0c;愿新年的钟声带给你一份希望和期待&#xff0c;我相信&#xff0c;时空的距离不能阻隔你我&#xff0c;我的祝…

Solidity 中的数学(第 5 部分:指数和对数

本文是关于在 Solidity 中进行数学运算的系列文章中的第五篇。这次的主题是&#xff1a;指数和对数 介绍 几个世纪以来&#xff0c;对数被用来简化计算。在电子计算器广泛普及之前&#xff0c;计算尺、基于对数的机械计算器是工程师职业的标志。 对数函数连同指数函数&#x…

【matplotlib】21.多图合并【python3、numpy、pandas、matplotlib完结】

#【matplotlib】21.多图合并 2023.1.20 python3、numpy、pandas、matplotlib完结 新年快乐&#xff01;&#xff01; 21.1 多合一显示 21.1.1 均匀分布 方法很简单 就是一张图 分几块 第一张占几块 从哪开始&#xff1b;第二张… plt.subplot() # 打开一个窗口 import ma…

Redis数据类型简介

目录 1、字符串(Strings) 1.1、底层实现 1.2、基本命令 1.3、应用场景 2、列表(Lists) 2.1、底层实现 2.2、基本命令 2.3、应用场景 3、集合(Sets) 3.1、底层实现 3.2、基本命令 3.3、应用场景 4、哈希(Hashes) 4.1、底层实现 4.2、基本命令 4.3、应用场景 5、…

Caché数据库简要记录

前一阵子&#xff0c;实施的小伙伴找过来问我用过 Cach 数据库吗&#xff1f;我当时有点懵&#xff1f;难道是 redis、memcached 之流&#xff1f;后来我特意去搜索了一下&#xff0c;还真是有一款数据库叫做 Cach Database。 这里做一下简单的记录吧&#xff0c;Cach 在医疗系…

Mine Goose Duck 0.1版本发布

我本次制作了一款MC版的Goose Goose Duck模组&#xff0c;游戏版本是Forge 1.19.2。 1.身份物品 熟悉鹅鸭杀的朋友都知道游戏中含有许多的身份&#xff0c;有好人、坏人、中立三个阵营&#xff0c;本次我加入了原作中的一些基本角色&#xff1a; 1.警长 你可以杀死怪物但错…

DFS(三)电话号码的字母组合

数组版本 哈希版本&#xff1a; 一、17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串&#xff0c;返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 给出数字到字母的映射如下&#xff08;与电话按键相同&#xff09;。注意 1 不对应任何字母。 示例 1&#x…

Elasticsearch7.8.0版本高级查询——范围查询文档

目录一、初始化文档数据二、范围查询文档2.1、概述2.2、示例一、初始化文档数据 在 Postman 中&#xff0c;向 ES 服务器发 POST 请求 &#xff1a;http://localhost:9200/user/_doc/1&#xff0c;请求体内容为&#xff1a; {"name":"张三","age"…

Git的安装与使用

目录 一、简介 1. Git 是什么&#xff1f; 2. 集中式和分布式 二、Git 环境搭建 1. Linux 下安装 2. Windows 下安装 三、创建版本库 四、把文件添加到版本库 1. 添加并提交 2. 仓库状态 3. 版本回退 &#xff08;1&#xff09;查看提交日志 &#xff08;2&#xff0…

量化交易-因子有效性分析

量化交易-因子有效性分析一、 因子的 IC 分析2. 信息系数3. 举例4. 因子处理4.1 去极值4.2 标准化4.3 市值中性化一、 因子的 IC 分析 判断因子与收益的相关性强度 分析结果 因子平均收益IC meanIC stdIC > 0.02&#xff1a;IC大约0.02的比例&#xff0c;越大越严格IR&…