Leetcode#191. Number of 1 Bits(位1的个数)
题目描述
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
示例 :
输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011
示例 2:
输入: 128
输出: 1
解释: 整数 128 的二进制表示为 00000000000000000000000010000000
思路
思路一:
用Integer.bitCount函数统计参数n转成2进制后有多少个1
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
思路二:
如果一个整数不为0,那么这个整数至少有一位是1。
如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。
其余所有位将不会受到影响。
思路三:
用flag来与n的每位做位于运算,来判断1的个数
代码实现
package BitManipulation;
/**
* 191. Number of 1 Bits(位1的个数)
* 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
*/
public class Solution191 {
public static void main(String[] args) {
Solution191 solution191 = new Solution191();
int n = 11;
System.out.println(solution191.hammingWeight(n));
}
/**
* 用Integer.bitCount函数统计参数n转成2进制后有多少个1
*
* @param n
* @return
*/
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
/**
* 如果一个整数不为0,那么这个整数至少有一位是1。
* 如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。
* 其余所有位将不会受到影响。
*
* @param n
* @return
*/
public int hammingWeight_2(int n) {
int count = 0;
while (n != 0) {
count++;
n = (n - 1) & n;
}
return count;
}
/**
* 用flag来与n的每位做位于运算,来判断1的个数
*
* @param n
* @return
*/
public int hammingWeight_3(int n) {
int count = 0;
int flag = 1;
while (flag != 0) {
if ((flag & n) != 0) {
count++;
}
flag = flag << 1;
}
return count;
}
}
Leetcode#191. Number of 1 Bits(位1的个数)的更多相关文章
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- leetcode 191 Number of 1 Bits(位运算)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] 191. Number of 1 Bits 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 191 Number of 1 Bits 位1的个数
编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量).例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以 ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- LeetCode 191. Number of 1 bits (位1的数量)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- pip install urllib3[secure] 报错 error: ffi.h: No such file or directory
解决 sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging pyt ...
- docker面试整理
为什么要使用docker https://www.cnblogs.com/AshOfTime/p/10755479.html docker的使用场景 docker和虚拟机比较的优势 https: ...
- 好程序员web前端开发测验之css部分
好程序员web前端开发测验之css部分Front End Web Development Quiz CSS 部分问题与解答 Q: CSS 属性是否区分大小写? <p><font si ...
- 【Atcoder Grand Contest 011 F】Train Service Planning
题意:给\(n+1\)个站\(0,\dots,n\),连续的两站\(i-1\)和\(i\)之间有一个距离\(A_i\),其是单行(\(B_i=1\))或双行(\(B_i=2\)),单行线不能同时有两辆 ...
- 5-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(配置保存数据的数据库)
配置信息如下:这是我的python软件和APP软件默认连接的配置 数据库名称:iot 编码utf8 表格名字:historicaldata 字段 id 自增,主键 date ...
- 点击button自动刷新页面的奇葩错误
以前在写练习的时候遇到过这样一个问题,自己在html中写了一个button <button>test1</button> 在没有给其附上onclick事件时是点击是不会有任何反 ...
- FAST MONTE CARLO ALGORITHMS FOR MATRICES II (快速的矩阵分解策略)
目录 问题 算法 LINEARTIMESVD 算法 CONSTANTTIMESVD 算法 理论 算法1的理论 算法2 的理论 代码 Drineas P, Kannan R, Mahoney M W, ...
- Web项目中出现乱码
(不知道怎么写才好) 分两种情况: 1.如果是 get 方式 单独修改: new String(str.getBytes("原来的编码"), "想要的编码") ...
- 小小知识点(四)——MATLAB如何画等高线图和线性规划约束方程
MATLAB程序: figure contourf(x,y,data) % 画等高线 hold on plot(x,y(x)) %画线性规划约束方程1 hold on plot(y,x(y)) %画线 ...
- RBAC权限管理模型 产品经理 设计
RBAC权限管理模型:基本模型及角色模型解析及举例 | 人人都是产品经理http://www.woshipm.com/pd/440765.html RBAC权限管理 - PainsOnline的专栏 ...