public class Solution {

// you need to treat n as an unsigned value

public int hammingWeight(int n) {

int c=0;

while(n>0)

{

c=c+n%2;

n=n/2;

}

if(n==1)

return c+1;

else

return c;

}

}

Input: 2147483648 (10000000000000000000000000000000)
Output: 0
Expected: 1

原因,题目要求int是无符号int型,而java中int是由符号的,因此当输入2147483648时为0而其表示是10000。。。000所以采用二进制的&运算,如下

public class Solution {

// you need to treat n as an unsigned value

public int hammingWeight(int n) {

int s=0;

while(n!=0)

{

s++;

n=n&(n-1);

}

return s;

}

}

转载于:https://blog.51cto.com/7061299/1639719

Leetcode#191Number of 1 Bits相关推荐

  1. leetcode Number of 1 Bits

    题目连接 https://leetcode.com/problems/number-of-1-bits/ Number of 1 Bits Description Write a function t ...

  2. [LeetCode] Number of 1 Bits Reverse Integer - 整数问题系列

    目录: 1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历] 3.Reverse Integer - 翻 ...

  3. leetcode 上的Counting Bits 总结

    最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那 ...

  4. [LeetCode]Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  5. [笔试题目] 美团2015年9月后端开发工程师笔试题

    由于题目是我通过草稿回顾,可能表述不清,但是内容大致一样.希望该博客内容对你有所帮助,题目所有权归美团公司所有,我只是想分享给大家学习,还望贵公司海涵~ 面试职位 应聘职位:后端开发工程师 岗位描述: ...

  6. 博弈论与SG函数(Nim游戏)

    博弈论与SG函数(Nim游戏) 目录 博弈论与SG函数(Nim游戏) 游戏状态 状态图(SG图) Nim 游戏 Nim 和 SG函数 Grundy数字 组合博弈游戏 Grundy 游戏 例题 在本篇, ...

  7. 感谢十二年的陪伴——分享回归,不忘初心(Eastmount博客总结及未来规划)

    曾记否,2021年4月28日,为了更好地从事科研和学习,当时给所有读者群发了我在CSDN唯一的私信,感谢大家十年的陪伴,短暂消失,不负青春.当时也收到了很多博友的鼓励与祝福,感恩. 是啊!很难想象读博 ...

  8. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  9. LeetCode - Easy - 191. Number of 1 Bits

    Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function ...

  10. leetcode python3 简单题191. Number of 1 Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...

最新文章

  1. 从业务出发,来谈谈策略模式,清爽的飞起~
  2. Oracle的sql 函数
  3. windows docker 卸载_安装Docker(Docker Toolbox)与Splash库
  4. 将protobuf文档转换成java代码
  5. 【springboot中使用拦截器】
  6. 网络爬虫--25.【selenium实战】实现拉勾网爬虫之--selenium获取数据
  7. django 1.8 官方文档翻译: 1-1-1 Django初探
  8. 死磕单点登录的实现原理....
  9. linux常用命令详解和用法_详解Linux shell用法中常用命令--read(传参)
  10. Fiddler环境配置、抓包、手机代理
  11. 吊打面试官之redis篇:一文全懂redis
  12. 科技爱好者周刊(第 165 期):全端 App 的时代
  13. 数理统计——随机过程
  14. (一百五十一)Android P 真正创建sta iface的地方
  15. CSS3-3D变换 transform-style:preserve-3d
  16. 【数据分析与挖掘实战】金融风控之贷款违约预测详解1(有代码和数据集)
  17. 《跟小智一起学网络》教程目录
  18. 情侣博客源码php,分享超炫的表白页面和爱的纪念日源码
  19. 开发脚手架及封装自动化构建工作流
  20. 信号与系统(三):系统分析方法对比:微分方程 相量 傅里叶级数/变换 拉普拉斯变换

热门文章

  1. Git版本控制:Git分支处理
  2. python字符串、字符串处理函数及字符串相关操作
  3. Aggregated Residual Transformations for DeepNeural Networks -ResNetXt2017【论文理解】
  4. C/C++ 创建两个链表,实现两个链表低位到高位相加,并输出链表
  5. VsCode 使用和问题总结
  6. 《Android开发艺术探索》— Android 书籍
  7. Android ViewPager 滑动监听事件
  8. Android SurfaceView动画(二)
  9. Android 实现 相机、相册功能 + 图片存取数据库操作
  10. 在python中打开文件显示没有权限PermissionError: [Errno 13] Permission denied: