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. android开发之Bundle使用

    android开发中,我们经常需要在两个activity之间传递数据,最常用的莫过于使用intent.putXXX(),可是很多时候我们也会这样: Bundle bundle = new Bundle ...

  2. android系统体系结构

    android系统底层是建立在Linux系统之上的,如下图 从上图可以看出android系统有五部分组成 1.APPLICATIONS(应用程序层) 包含一些核心应用程序,电子邮件,日历,地图,浏览器 ...

  3. DNS(域名系统)域名解析设置

    DNS(Domain Name System,域名系统), 因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最 ...

  4. Access的转义字符

    Access中数据库转义字符规则: 插入.更新.=匹配 数据时,文本类型如用''括起来,中间可以有 ",*,%,[,],/,/,?,(,),{,}的任意组合,如要插入一个',需写''并在整个 ...

  5. double 类型运算会出现精度问题

    要先转换为字符串,后进行运算,可以写个方法做乘法运算public static double mul(double v1,double v2){BigDecimal b1 = new BigDecim ...

  6. MORE ABORT AWR?

    For some time, Oracle’s solution in this area has been its built-in tool, Statspack.Oracle Database ...

  7. android网络图片查看器

    package com.itheima.netimageviewer; import java.io.BufferedReader; import java.io.File; import java. ...

  8. MySQL常见问题汇总(原创)

    本文记录了使用Mysql时遇到的问题,持续更新中... 1.在windows命令行下登录mysql时报错: C:\Program Files\MySQL\MySQL Server 5.0\bin> ...

  9. iOS 十六进制的相加取反

    ios中将NSstring字符串转换成char类型 NSString *string = [NSString stringWithFormat:@"5D"]; const char ...

  10. iOS UITableviewWrapperView 和 automaticallyAdjustsScrollViewInsets属性

    关于在navigationController下面使用tableView在竖直方向会遇到frame的y值的困惑, 会遇到视图控制器的这个属性:automaticallyAdjustsScrollVie ...