[LC] 191. Number of 1 Bits
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
Example 1:
Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer
-3.
Follow up:
If this function is called many times, how would you optimize it?
Solution 1:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
// cannot use n > 0 if n=2147483648
while (n != 0) {
n &= n - 1;
res += 1;
}
return res;
}
}
Solution 2:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
for (int i = 0 ; i < 32; i++) {
if ((n & 1) == 1) {
res += 1;
}
n = n >> 1;
}
return res;
}
}
[LC] 191. Number of 1 Bits的更多相关文章
- 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的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- 191. Number of 1 Bits 二进制中1的个数
[抄题]: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- 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 ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
- Java [Leetcode 191]Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- 191. Number of 1 Bits
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- 191. Number of 1 Bits Leetcode Python
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also know ...
随机推荐
- Pyspider的简单介绍和初使用
Pyspider Pyspider是由国人(binux)编写的强大的网络爬虫系统 Ptspider带有强大的WebUi / 脚本编辑器 / 任务监控器 / 项目管理器以及结果处理器.他支持多种数据 ...
- MBProgressHUD覆盖键盘
发送消息的时候,需要用MBProgressHUD来覆盖窗口等待发送成功,但是无论如何键盘都覆盖不上. 于是各种研究,添加到view,添加到window,都无果..想破了脑子,总觉得加到window都覆 ...
- 三大PLM厂商
西门子的叫Teamcenter(汽车.通用机械) 法国达索的Enovia(航空\汽车\高铁等交通运输行业和机械行业) PTC的叫windchill(船舶.电子)
- fscanf的用法
fscanf用于读取字符串数据流,遇到空白字符(空格' '; 制表符'\t'; 新行符'\n')就停止,若要读取完整的一行数据,可以使用格式控制("%[^\n]%*c"),或者使用 ...
- \_\_module\_\_和\_\_class\_\_
目录 __module__和__class__ 一.__module__ 二.通过字符导入模块 三.__class__ __module__和__class__ # lib/aa.py class C ...
- 简单的tab栏切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用mha 构建mysql高可用碰到几个问题
根据网上配置,安装好mha ,建议到https://code.google.com/archive/p/mysql-master-ha/downloads 下载0.56版本 1.首先先确定各个主机之 ...
- 001-rabbitmq和haproxy结合
rabbitmq集群搭建 防火墙添加并重启 -A INPUT -p tcp -m multiport --dports 4369,25672,5672,15672 -j ACCEPT 安装 cd /u ...
- vue中axios的post请求使用form表单格式发送数据
vue使用插件qs实现 (qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库.) 在jquery中的ajax的方法已将此封装,所以不需要再次序列化 1. 安装 在项目中使用命令行工具输 ...
- 微软不将《帝国时代》终极版上架Steam的原因到底是什么?
毋庸置疑的是,<帝国时代>绝对是一款经典游戏.作为一款RTS名作,在过去的20年时间中<帝国时代>销量超过2000万部.数以千万计的玩家都沉溺于这款游戏中,<帝国时代&g ...