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. 从零开始学习jquery (一)

    一.jquery是什么 Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safar ...

  2. TCP/IP协议原理与应用笔记11:TCP/IP中地址与层次关系

    1. 网络中常用的地址: 2. TCP/IP中地址与层次关系 :

  3. codevs 1128 导弹拦截 (贪心)

    /* 题目大体意思是两套系统好多导弹 怎样分配使得两个系统所拦截的最大半径之和最小 贪心:把距离1系统最远的 让2拦截 记好距离 然后按照距离1由远到近排序 对于每一个导弹 如果这之前的都给2拦截 则 ...

  4. 兼容所有浏览器的JQuery zClip插件实现复制到剪贴板功能

    相信这个功能大家平时上网经常能碰到,以前也没怎么留意怎么实现的,直到项目中需要. 网上一搜一大堆,单纯使用js方法也不是没有,但是由于各浏览器的安全机制不同,不是跨浏览器的.去看了几个常用的网站,都是 ...

  5. MVC跳转

    //RedirectToAction(view?参数,控制器); return RedirectToAction("MyjoinEvent?id=" + eventid + &qu ...

  6. javascript判断值是否undefined

    function isUndefined(variable) { return typeof variable == 'undefined' ? true : false; }

  7. [转]最详细的 HTTPS 科普扫盲帖

    转载自http://www.codeceo.com/article/https-knowledge.html 为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都 ...

  8. java中集合类的简介

    结构 collection(接口) List(接口) LinkedList(类) ArrayList(类) Vector(类) Stack(类) Set(接口) Map(接口) Hashtable(类 ...

  9. B/S 獲取客戶端Mac地址

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx. ...

  10. cursor 与refcursor及sys_refcursor的区别 (转载)

    楼主标明是转载的,我只把我转载的地址发上来 http://www.cnblogs.com/honliv/archive/2011/07/21/2112639.html 显式是相对与隐式cursor而言 ...