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.

接口

public int hammingWeight(int n);

2 思路

统计出一个数用二进制表示的时候,里面'1'的个数。

Reverse Bits思路一样,位运算。

复杂度

Time: O(n)  Space: O(1)

3 代码

     public int hammingWeight(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
final int bit = (n >> i) & 1;
res += bit;
}
return res;
}

4 总结

Reverse Bits思路一样。

5 参考

  1. leetcode
  2. Leetcode: Number of 1 Bits

lc面试准备:Number of 1 Bits的更多相关文章

  1. [LC] 191. Number of 1 Bits

    Write a function that takes an unsigned integer and return the number of '1' bits it has (also known ...

  2. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

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

  3. 191. Number of 1 Bits 二进制中1的个数

    [抄题]: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

  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] 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】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  7. Number of 1 Bits(Difficulty: Easy)

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

  8. LeetCode 191 Number of 1 Bits

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

  9. LeetCode Number of 1 Bits

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

随机推荐

  1. 百度知道的php爬虫

    原文地址:百度知道的php爬虫作者:好宏杰软件 <?php class spider  {    private $content ;    private $contentlen ;    p ...

  2. HTML5吧

    一.为了能使IE9以下的IE浏览器也能支持html5的标签,所以首先得在文档头部用条件注释的方法引入那段著名的代码. 1 2 3 <!--[if lt IE 9]> <script ...

  3. U3D 精灵的点击监听

    U3D游戏中,可能会用到点击对象,完成某项操作, 方法一:可以通过接收Input对象的输入,进行利用 方法二:给对象绑定一个collier 组件,然后就能后使用内置方法 这里有点不同,方法一,是不管哪 ...

  4. Java SE (1)之 JFrame 组件 BorderLayout 布局

    JAVA 初期,练习SE ,桌面程序, package com.sunzhiyan; import java.awt.*; import java.awt.event.*; import javax. ...

  5. [XML] C#XMLProcess操作Xml文档的帮助类 (转载)

    点击下载 XMLProcess.rar 主要功能如下所示 看下面代码吧 /// <summary> /// 类说明:XMLProcess /// 编 码 人:苏飞 /// 联系方式:361 ...

  6. 多线程、Socket

      多线程 线程.进程和应用程序域 进程:进程是一个操作系统上的概念,用来实现多任务并发执行,是资源分配的最小单元,各个进程是相互独立的,可以理解为执行当中的程序,在操作系统中一般用一个称为PCB的结 ...

  7. CSS 创建

    当读到一个样式表时,浏览器会根据它来格式化 HTML 文档. 如何插入样式表 插入样式表的方法有三种: 外部样式表 内部样式表 内联样式 外部样式表 当样式需要应用于很多页面时,外部样式表将是理想的选 ...

  8. Java RMI 框架_远程方法调用(2016-08-16)

    概念: Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可 ...

  9. 关于移动div的具体实现(js)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. video,source元素

    一,视频 <video src="../[再一次快乐结局]第15集.mp4" controls="controls" width="500&qu ...