当前位置:网站首页>- 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是多么幸福的一件事,各位,共勉!!
边栏推荐
猜你喜欢

【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?

NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...

MySql中@符号的使用

Mysql execution principle analysis

【HMS core】【FAQ】HMS Toolkit典型问题合集1

CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri

ByteArrayInputStream class source code analysis

Mongo for infrastructure

时序数据库在船舶风险管理领域的应用

使用postman调接口报Content type ‘text/plain;charset=UTF-8‘ not supported
随机推荐
ESP8266-Arduino programming example-BMP180 air pressure temperature sensor driver
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
ctf.show_web5
使用postman调接口报Content type ‘text/plain;charset=UTF-8‘ not supported
OSPF详解(3)
强啊,点赞业务缓存设计优化探索之路。
第4章 控制执行流程
LeetCode 练习——关于查找数组元素之和的两道题
载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
Two-point answer naked question (plus a little pigeonhole principle)
ROS 环境使用第三方动态链接库(.so)文件
kotlin的by lazy
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
好未来单季营收2.24亿美元:同比降84% 张邦鑫持股26.3%
ROS 节点初始化步骤、topic/service创建及使用
基于inquirer封装一个控制台文件选择器
一文读懂“语言模型”
ByteArrayInputStream 类源码分析
尊重客观事实
固定资产可视化智能管理系统

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