当前位置:网站首页>- daily a LeetCode 】 【 191. A number of 1
- daily a LeetCode 】 【 191. A number of 1
2022-07-30 18:35:00 【The moon chews into stars~】
作者简介:一名大一在校生
个人主页:月亮嚼成星~
个人WeChat:yx1552029968
系列专栏:每日一道LeetCode
每日一句: 决定一个人成就的,不是靠天,也不是靠运气,而是坚持和付出,是不停地做,重复的做,用心去做,当你真的努力了付出了,你会发现自己潜力无限!
目录
原题:位1的个数
编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量).
示例 1:
输入:00000000000000000000000000001011
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'.
示例 2:
输入:00000000000000000000000010000000
输出:1
解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'.
示例 3:
输入:11111111111111111111111111111101
输出:31
解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'.
提示:This problem involves the knowledge of binary bit operations and shift operations,Those who don't know the iron juice can look at this blogger's article,A brief introduction to the relevant knowledge,Help us do the quiz:【Java SE]位运算和移位运算注意事项
解题思路:
方法一:Violent displacement solution
为了记录1的位数,We need to set up a countercount并将其初始化为0;In order to find out whether a position is 1,To use bitwise AND(&)1来进行操作,如果是1,结果就是1,不是1,结果为0.This is just a positional comparison,考虑到有32位,So we use bitwise operations to implement each bitwise AND1进行比较,available after the cycle1的数. 为了便于理解,Draw a picture for the first comparison,The latter comparison method is the same.
Of course there are better solutions to this problem,With an open mind,We will think of more good solutions!

代码执行:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count=0;//计数器
for(int i=31;i>=0;i--){
if(((n>>i)&1)==1){
count++;
}
}
return count;
}
}
- 复杂度分析:时间复杂度
O(k),k=32.空间复杂度为O(1)
执行结果:

方法2:优化循环的过程
思路:巧用二进制公式x&(x-1)表示去掉二进制中最右边的第一个1,加速循环过程
代码执行:
public class Solution {
public int hammingWeight(int n) {
int ret = 0;
while (n != 0) {
n &= n - 1;
ret++;
}
return ret;
}
}
运行结果:
复杂度分析:时间复杂度为O(k),k为二进制中1的个数,最坏的情况下所有位都是1.空间复杂度是O(1)
总结:
Brush one every dayLeetCode是多么幸福的一件事,各位,共勉!!
边栏推荐
猜你喜欢

【PHPWord】PHPOffice 套件之PHPWord快速入门

MySql中@符号的使用

ByteArrayInputStream 类源码分析

The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!

Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"

LeetCode Exercise - Two Questions About Finding Sum of Array Elements

Pytorch foundation -- tensorboard use (1)

LeetCode 练习——关于查找数组元素之和的两道题

Basic use of scrapy

OneFlow源码解析:Op、Kernel与解释器
随机推荐
AWS console
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
[Use of Qt Designer tool]
requet.getHeader(“token“) 为null
【Prometheus】Prometheus联邦的一次优化记录[续]
Presto 中 lookUp Join的实现
Common linked list problems and their Go implementation
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
时序数据库在船舶风险管理领域的应用
【Qt Designer工具的使用】
[OC study notes] attribute keyword
基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
ESP8266-Arduino编程实例-BMP180气压温度传感器驱动
LeetCode 练习——关于查找数组元素之和的两道题
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
固定资产可视化智能管理系统
Read the "Language Model" in one article
Recommendation | People who are kind to you, don't repay them by inviting them to eat
Anaconda Navigator卡在loading applications

作者简介:
个人主页:
个人WeChat:
系列专栏:
每日一句: