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

Example 1:

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

Example 2:

Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.

Example 3:

Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.

Follow up:

If this function is called many times, how would you optimize it?

Solution 1:

 public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
// cannot use n > 0 if n=2147483648
while (n != 0) {
n &= n - 1;
res += 1;
}
return res;
}
}

Solution 2:

 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) {
res += 1;
}
n = n >> 1;
}
return res;
}
}

[LC] 191. Number of 1 Bits的更多相关文章

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

  2. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  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. LeetCode 191 Number of 1 Bits

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

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

  6. (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 ...

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

  8. 191. Number of 1 Bits

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

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

随机推荐

  1. Python笔记_第四篇_高阶编程_进程、线程、协程_3.进程vs线程

    1.多任务的实现原理: 通常我们会设计Mater-Workder模式,Master负责分配任务,Worker负责执行任务,因此多任务环境下,通常是一个Master,多个Worker 2.多进程: 主进 ...

  2. web项目servlet&jsp包失效问题

    今天偶然遇到这样的一个问题,故做个总结. javaee开发只用到serlet和jsp两个包.而sun提供的jdk只是javase部分的包,对于se部分只提供了规范,而包由容器给出. 由于自己在新建好一 ...

  3. MySQL和Java数据类型对照表

    Java MySQL数据类型对照 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+N VARCHAR java.lang.S ...

  4. 2019牛客暑期多校训练营(第五场)B.generator 1

    传送门:https://ac.nowcoder.com/acm/contest/885/B 题意:给出,由公式 求出 思路:没学过矩阵快速幂.题解说是矩阵快速幂,之后就学了一遍.(可以先去学一下矩阵快 ...

  5. java常用工具类(三)

    一.连接数据库的综合类 package com.itjh.javaUtil; import java.sql.Connection; import java.sql.DriverManager; im ...

  6. 11)PHP,单选框和复选框的post提交方式处理

    就是一个表单中会有input的checkbox形式,那么怎么处理,就有了问题,一般采用二维数组来处理 代码展示: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  7. C# 创建、部署和调用WebService的简单示例 (转)

    C# 创建.部署和调用WebService的简单示例(转)  转自 https://www.cnblogs.com/Brambling/p/7266482.html  webservice 可以用于分 ...

  8. c语言中continue的运用,同时学习接收字符,打印字符,遍历字符

    /************************************************************************* > File Name: continue. ...

  9. html 基础笔记

    html 1,一套规则,浏览器认识的规则 2,开发者: 学习Html规则 开发后台程序: -写Html文件(充当模板的作用) -数据库获取数据,然后替换到html文件的指定位置(web框架) 3,本地 ...

  10. jQuery方法及使用

    jQuery内容: 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each.data.Ajax 剩余未写的有: 1.表单筛选器: :text :password :fi ...