(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 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
参考编程之美120页
方法1:使用位操作
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
num+=(n&1);
n=n>>>1;
}
return num;
}
}
运行结果:时间复杂度为O(logV),即二进制位数。

方法二:n&=(n-1)
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
n&=(n-1);
num++;
}
return num;
}
}
运行结果:

(easy)LeetCode 191.Number of 1 Bits的更多相关文章
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- 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] 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 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 ...
- 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 ...
- [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 ...
随机推荐
- mysql将字符转换成数字
在操作mysql时,经常需要将字符转换成数字,这一步虽然简单,但不常用的话也很容易忘记,现将在网上找到的方法记录如下: 1.将字符的数字转成数字,比如'0'转成0可以直接用加法来实现例如:将pony表 ...
- flex html 用flex展示html
1. 目的 flex展示html 可以保护网页内容 2. 参考 http://stackoverflow.com/questions/260270/display-html-in-an-actions ...
- json字符串转map
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...
- hadoop(四): 本地 hbase 集群配置 Azure Blob Storage
基于 HDP2.4安装(五):集群及组件安装 创建的hadoop集群,修改默认配置,将hbase 存储配置为 Azure Blob Storage 目录: 简述 配置 验证 FAQ 简述: hadoo ...
- 【shell】read
read:read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数据放入一个标准变量中. [参数][变量] 注意:变量要在参数的后面 主要参数: -t ...
- ajaxForm笔记
<script src="Scripts/jquery.form.js" type="text/javascript"></script> ...
- android学习笔记37——Menu资源
Menu菜单资源 android应用推荐使用XML来定义菜单,其可提供更好的解耦方式. 菜单资源通常位于res/menu文件夹下,其菜单根元素为<menu.../>,menu元素下可包含子 ...
- apidoc,一个相当不错的文档生成器
http://apidocjs.com/ 例子:myapp目录下的MyCode.java /** * * @api {get} /company/list 获取公司信息 * @apiName 获取公司 ...
- erlang远程加载模块须知
erlang加载本地beam到远程节点,需要把依赖库一个个手动加载,否则他不会自动加载. 另外,创建lib的话,使用 rebar-creator create-lib
- 基于session的简易购物车引发的问题
一.功能描述: 页面如下所示: 运行报错: java.io.FileNotFoundException: E:\apache-tomcat-8.0.37\work\Catalina\localhos ...