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.

题目大意:给一个无符号数,返回这个数二进制表示中1的数量。

解题思路:按位和0x1与操作,计算即可。

    public int hammingWeight(int n) {
int res = 0;
while(n!=0){
if((n&0x1)==1){
res++;
}
n=n>>>1;
}
return res;
}

Number of 1 Bits——LeetCode的更多相关文章

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

  2. 693. Binary Number with Alternating Bits - LeetCode

    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...

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

  4. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

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

  6. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  7. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  8. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  9. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

随机推荐

  1. Linux TCP队列相关参数的总结

    作者:阿里技术保障锋寒 原文:https://yq.aliyun.com/articles/4252 摘要: 本文尝试总结TCP队列缓冲相关的内核参数,从协议栈的角度梳理它们,希望可以更容易的理解和记 ...

  2. Enterprise Architect使用教程

    一.Enterprise Architect简介 Enterprise Architect是一个对于软件系统开发有着极好支持的CASE软件(Computer Aided Software Engine ...

  3. Linux cpuinfo 详解

     在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 经过查看,我的开发机器是1个物理C ...

  4. Monitor and diagnose performance in Java SE 6--转载

    Java SE 6 provides an in-depth focus on performance, offering expanded tools for managing and monito ...

  5. 【IOS】 XML解析和xml转plist文件(GDataXML)

    iOS对于XML的解析有系统自带的SDK--NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用.(以下图为例) 1.对于一个xml文件,先读取出来 NSDat ...

  6. python环境准备

    一.环境准备. 1.安装python3.5.2(勾选环境变量),python2.7.12 2.设置环境变量 (要求命令行输入python,进入python2命令行,打python3时,进入python ...

  7. Mysql 中和同to_char 一样用法的函数

    STR_TO_DATE() $sql = " SELECT "; $sql .= " m_img,m_content,STR_TO_DATE(m_time,\" ...

  8. 认识html文件基本结构

    html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <body>...</body> ...

  9. Swift - 22 - 循环结构

    //: Playground - noun: a place where people can play import UIKit // for-in for i in -99...99 { i * ...

  10. 数据类型的转换String

    x.toString(): 无法转换null和undefined 不过String()却是万能的,其中的原理如下 function String(x){ if(x===undefined){ retu ...