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 ...
随机推荐
- supervisor 工具 配置
配置supervisor工具,管理django后台 supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可 ...
- http协议进阶(五)连接管理
几乎所有的HTTP通信都是由TCP/IP承载的,TCP/IP是全球计算机网络设备都在使用的一种分组交换网络分层协议集. 它的特点是只要连接建立,客户端与服务器之间的报文交换就永远不会丢失.受损或失序. ...
- python之zip打包
import zipfile # 压缩 z = zipfile.ZipFile('z.zip', 'w') z.write('xo.xml') z.write('xxxoo.xml') z.close ...
- 在项目中迁移MS SQLServer到Mysql数据库,实现MySQL数据库的快速整合
在开发项目的时候,往往碰到的不同的需求情况,兼容不同类型的数据库是我们项目以不变应万变的举措之一,在底层能够兼容多种数据库会使得我们开发不同类型的项目得心应手,如果配合快速的框架支持,那更是锦上添花的 ...
- MariaDB第三章:数据库设计与备份--小白博客
数据库设计 1.第一范式(确保每列保持原子性) 第一范式是最基本的范式.如果数据库表中的所有字段值都是不可分解的原子值,就说明该数据库表满足了第一范式. 2.第二范式(确保表中的每列都和主键相关) 第 ...
- Scrapy安装报错
python3 pip 安装Scrapy在win10 安装报错error: Microsoft Visual C++ 14.0 is required. Get it with "Micro ...
- 妙谈js回调函数的理解!
很有共鸣,之前也是一直对回调函数感觉不明不白的,自己也看了不少解释说明.后来我觉得造成很多人对回调理解困难的一个原因就是,我在开发中见到的大多数使用了回调函数的情况都是直接上来就 传一个回调函数进去 ...
- Linux(Ubuntu)使用日记------自定义命令的使用
Linux如何自定义自己的命令呢?修改 系统中的 ~/.bashrc 文件即可 在这个文件最后面使用alias命令重定义命令. 例如: # novel-git begin alias n.r='les ...
- centos7之zabbix3.2搭建
环境介绍: centos7.4 zabbix3.2 一.zabbix介绍 zabbix官网:https://www.zabbix.com/ zabbix下载页面:https://www.zabbix. ...
- 精心收集的 95 个超实用的 JavaScript 代码片段( ES6+ 编写)
https://www.html.cn/archives/8748#table-of-contents https://www.haorooms.com/post/js_regexp