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.


题目标签:Bit Manipulation

  这题给我们一个integer,让我们找到bits中1的个数,利用 & 1 判断bit是不是1,然后利用 >> 来移动bits。

Java Solution:

Runtime beats 16.07%

完成日期:06/26/2017

关键词:Bit Manipulation

关键点:用 & 拿到bit, 用 >> 来移动bits

 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) // meaning the most right bit is 1
res++; n = n >> 1; // shift to right 1 bit
} return res;
}
}

参考资料: N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 191. Number of 1 bits (位1的数量)的更多相关文章

  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 ...

  2. 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 ...

  3. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  4. 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 ...

  5. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  6. [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 ...

  7. 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 ...

  8. (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 ...

  9. 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 ...

随机推荐

  1. lintcode.67 二叉树中序遍历

    二叉树的中序遍历    描述 笔记 数据 评测 给出一棵二叉树,返回其中序遍历 您在真实的面试中是否遇到过这个题? Yes 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3, ...

  2. spring的一些问题

    1.什么是spring? spring是一个轻量级的一站式框架,它的核心有两个部分,1.aop面向切面编程 2.ioc控制反转. 2.什么是aop aop就是面向切面编程,使用aop可以使业务逻辑各个 ...

  3. Wrong FS: hdfs://......, expected: file:///

    单机版使用的是FileSystem类的静态函数: FileSystem hdfs = FileSystem.get(conf) 伪分布式下需要使用Path来获得 Path path = new Pat ...

  4. 【个人笔记】《知了堂》前端mySql基础

    指定列之后添加: ALTER TABLE 表名 ADD 添加的新列名 INT AFTER 指定列之后 第一个位置: ALTER TABLE 表名 ADD 添加的新列名 varchar(20) AFTE ...

  5. axios 学习笔记

    官方文档地址:https://github.com/axios/axios axios 是一个基于 Promise 的HTTP库,可以用在浏览器和 node.js 中 特性: • 从浏览器发起 XML ...

  6. 调用惯例Calling Convention (或者说:调用约定)

    调用惯例影响执行效率,参数的传递方式以及栈清除的方式.   调用惯例 参数传递顺序 谁负责清除参数 参数是否使用暂存器 register 从左到右 被调用者 是 pascal 从左到右 被调用者 否 ...

  7. 【重点突破】——Cookie的使用

    cookie:小甜饼 cookie:保存客户端浏览器中一个纯文本文件 版本高的浏览器可查看   F12->Resource  左下方cookie    查看 cookie作用: 保存:[安全性要 ...

  8. bzoj1087 [SCOI2005][状压DP] 互不侵犯King (状压)

    在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包含两个数N,K ( 1 ...

  9. java数据库编程之DAO模式

    第八章:DAO模式 8.1:JDBC封装 为了提高代码的维护性和扩展性,我们使用JDBC进行封装数据, 先定义统一的API,将操作数据的代码抽象到接口中,业务逻辑代码只需要调用这些接口的实现类的对象, ...

  10. Spring读书笔记——bean加载

    我们的日常开发几乎离不开Spring,他为我们的开发带来了很大的便捷,那么Spring框架是如何做到方便他人的呢.今天就来说说bean如何被加载加载. 我们在xml文件中写过太多类似这样的bean声明 ...