Leetcode the number of '1' bit
来源:互联网 发布:暴漫官方淘宝店是哪个 编辑:程序博客网 时间:2024/06/06 07:47
计算无符号整形数中 the number of ‘1’ bit,
这道题很基础,用到几个运算符,&,>>>
第一种利用java函数法
/*Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.*/public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { return Integer.bitCount(n); }}
接下来两种:
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int x) { int count = 0; while(n != 0) { count += n&1; n = n>>>1; } return count; }}
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int x) { int count = 0; while(n != 0) { if(n%2 != 0) count ++; n = n>>>1; } return count; }}
一天一题(虽然很菜,但要坚持嘛)
参考资料:
http://15838341661-139-com.iteye.com/blog/1642525
0 0
- Leetcode the number of '1' bit
- leetcode之Number of 1 Bit
- [leetcode][bit] Number of 1 Bits
- <LeetCode><Easy> 191 Number Of 1 Bit
- Posts Tagged 【bit】Number of 1 Bits
- Bit Manipulation Number of 1 Bits
- Bit Manipulation - Number of 1 Bits
- leetcode:count the number of nodes
- [leetcode][bit] Single Number
- LeetCode:Number of 1 Bits
- LeetCode-Number of 1 Bits
- leetcode: Number of 1 Bits
- LeetCode Number of 1 Bits
- Leetcode: Number of 1 Bits
- Leetcode--Number of 1 Bits
- [LeetCode] Number of 1 Bits
- [leetcode]Number of 1 Bits
- Leetcode-Number of 1 Bits
- 关于virtualbox安装linux无法识别硬盘的解决方案
- Objective-C动态性
- Search Paths 说明
- Django静态文件处理【开发环境】
- 【Android一般进阶】引用数据类型euqals()与==作用相同
- Leetcode the number of '1' bit
- Linux下程序设计基础
- 如何高效地判断数组中是否包含某特定值
- 递归函数,输入一个非负整数,返回组成它的数字之和。
- struts2的默认prepare拦截器
- MySQL远程访问设置终极方法
- Boosted Tree
- SwipeRefreshLayout组件 自己实现ListView下拉刷新
- LeetCode:Remove Linked List Elements