LeetCode_191. Number of 1 Bits
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?
package leetcode.easy;
public class NumberOf1Bits {
	// you need to treat n as an unsigned value
	public int hammingWeight1(int n) {
		int bits = 0;
		int mask = 1;
		for (int i = 0; i < 32; i++) {
			if ((n & mask) != 0) {
				bits++;
			}
			mask <<= 1;
		}
		return bits;
	}
	public int hammingWeight2(int n) {
		int sum = 0;
		while (n != 0) {
			sum++;
			n &= (n - 1);
		}
		return sum;
	}
	@org.junit.Test
	public void test() {
		System.out.println(hammingWeight1(-3));
		System.out.println(hammingWeight2(-3));
	}
}
LeetCode_191. Number of 1 Bits的更多相关文章
- [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】Number of 1 Bits
		
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
 - Number of 1 Bits(Difficulty: Easy)
		
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
 - LeetCode 191 Number of 1 Bits
		
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
 - LeetCode Number of 1 Bits
		
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
 - leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
		
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
 - 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 ...
 - leetcode:Number of 1 Bits
		
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
 
随机推荐
- ui自动化测试 SeleniumBase
			
ui自动化 SeleniumBase SeleniumBase是一个自动化web测试框架,它的设计pyse相似,基于selenium和unittest封装的框架,api多,支持命令行多参数执行 文档地 ...
 - idea远程debug调试阿里云ECS
			
1.首先远程服务器的代码跟本地项目代码应该完全一致,否则会出现debug混乱现象,亲测. 2.config如图: ①命名可以省略②复制这个地址③输入远程ip和自定义且未被占用的端口号xxxx 3.开放 ...
 - Mybatis的简单搭建
			
1.官方网址 http://www.mybatis.org/mybatis-3/zh/getting-started.html 2.导入jar包 3.根据官方文档,首先写mybatis-config. ...
 - Nginx反爬虫: 禁止某些User Agent抓取网站
			
问题 之前客户能够正常访问的一个网站这几天访问很慢,甚至有时候还拒绝访问.通过Nginx访问日志排查,发现有大量的请求指向同一个页面,而且访问的客户端IP地址在不断变化且没有太多规律,很难通过限制IP ...
 - [APIO2012]派遣 左偏树
			
P1552 [APIO2012]派遣 题面 考虑枚举每个节点作为管理者,计算所获得的满意程度以更新答案.对于每个节点的计算,贪心,维护一个大根堆,每次弹出薪水最大的人.这里注意,一旦一个人被弹出,那么 ...
 - 【luogu4474王者之剑】--网络流
			
题目描述 这是在阿尔托利亚·潘德拉贡成为英灵前的事情,她正要去拔出石中剑成为亚瑟王,在这之前她要去收集一些宝石. 宝石排列在一个n*m的网格中,每个网格中有一块价值为v(i,j)的宝石,阿尔托利亚·潘 ...
 - luogu P3709 大爷的字符串题
			
二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...
 - (转)代码审计利器-RIPS实践
			
一.代码审计工具介绍 代码审计工具可以辅助我们进行白盒测试,大大提高漏洞分析和代码挖掘的效率. 在源代码的静态安全审计中,使用自动化工具辅助人工漏洞挖掘,一款好的代码审计软件,可以显著提高审计工作的效 ...
 - <frame>、<iframe>、<embed>、<object> 和 <applet>
			
frame frame 必须在 frameset 里,而 frameset 又不能和 body 共存(就是一旦存在 frame,就不能存在 body 了,因此这个基本每人使用) 推荐阅读:https: ...
 - 下载MAMP
			
下载https://www.mamp.info/en/downloads/ MAMP PRO will create copies of the MySQL databases located in ...