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 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的数量)的更多相关文章
- [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的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '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
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- [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 ...
- 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 ...
随机推荐
- openfire:openfire单独编译指定插件的方法
方法一: openfire默认编译时,是同时编译主程序和所有在plugins目录中的插件.但进行插件开发时,这样编译就太费时费力.使用ant plugins可以只编译插件,能够节省部分时间.最节省时间 ...
- 最近使用 .NET Core 遇到的一些坑
最近.NET Core升级到2.0后开始慢慢捣鼓的多了起来,但遇到了不少坑,所以特来记录下. 第一个坑 条件编译符 我们在编写一些方法的时候通常会为Debug模式增加一些输出日志等以便我们检查,也会 ...
- 一篇搞定微信分享和line分享
前言 在h5的页面开发中,分享是不可或缺的一部分,对于一些传播性比较强的页面,活动页之类的,分享功能极为重要.例如,京东等电商年末时会有一系列的总结h5在微信中传播,就不得不提到微信的分享机制. 微信 ...
- 使用VUE模仿BOSS直聘APP
一.碎碎念: 偶尔在群里看到一个小伙伴说:最近面试的人好多都说用vue做过一个饿了么.当时有种莫名想笑. 为何不知道创新一下?于是想写个DEMO演练一下.那去模仿谁呢?还是BOSS直聘(跟我没关系,不 ...
- 用vue开发一个app(3,三天的成果)
前言 一个vue的demo 源码说明 项目目录说明 . |-- config // 项目开发环境配置 | |-- index.js // 项目打包部署配置 |-- src // 源码目录 | |-- ...
- Java多线程Runnable与Callable区别与拓展
我们先来分别看一下这两个接口 Runnable: // // Source code recreated from a .class file by IntelliJ IDEA // (powered ...
- java 反射 类装载器
前言: java语言允许通过程序化的方式间接对Class进行操作,Class文件由类装载器装载后,在jvm中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息,如构 ...
- python 库之lxml安装 坑一个
error: command 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python ...
- 【转】NoClassDefFoundError和ClassNotFoundException
调试Hadoop源码时,一运行就报这个错误,后来发现是maven配置时,scope配置的问题, MAVEN Scope使用 相关链接:http://acooly.iteye.com/blog/178 ...
- 移植Linux-3.4.2内核到S3C2440
一.BootLoader引导内核过程 1.Bootloader的工作 1.1.将内核读入内存 2.2.保存内核启动参数到指定位置,内核启动时去这个位置解析参数 3.3. ...