191. Number of 1 Bits

Easy

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?

package leetcode.easy;

public class NumberOf1Bits {
// you need to treat n as an unsigned value
public int hammingWeight1(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
bits++;
}
mask <<= 1;
}
return bits;
} public int hammingWeight2(int n) {
int sum = 0;
while (n != 0) {
sum++;
n &= (n - 1);
}
return sum;
} @org.junit.Test
public void test() {
System.out.println(hammingWeight1(-3));
System.out.println(hammingWeight2(-3));
}
}

LeetCode_191. Number of 1 Bits的更多相关文章

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

  2. 【leetcode】Number of 1 Bits

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

  3. Number of 1 Bits(Difficulty: Easy)

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

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

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

  6. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

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

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

  9. leetcode:Number of 1 Bits

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

随机推荐

  1. (C99)复合字面量

    #include <stdio.h> struct argvs { char a[64]; int b; }; int pfunc(struct argvs *a) { printf(&q ...

  2. js 数组中如何删除字典

    区别: []表示是一个数组,如var strs = ['a','b','c'].{}表示是一个对象,比如,var obj = {name: '宙斯',sex: 1} 如何在数组中删除指定对象呢?? [ ...

  3. Spring事务管理器

    1.创建实体和接口 public class Bank { private Integer id; private String name; private String manay; public ...

  4. 国庆集训 Day1 T2 生成图 DP

    国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...

  5. linux系列(十二):more命令

    1.命令格式: more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file] 2.命令功能: more命令和cat的功能一样都是查看文件里的内容,但 ...

  6. CRNN网络结构详解

    目录 一. CRNN概论 简介 网络 二. CRNN局部之特征提取 三. CRNN局部之BLSTM 四. CRNN局部之CTC 关于CTC是什么东西? CTC理论基础 五. 参考文献 一. CRNN概 ...

  7. 30行左右代码实现一个类似httprunner的接口框架

    框架的最终归宿往往是领域语言+模板解析. 首先先约定一种所要执行操作的表述格式.然后通过模板解析将描述语言转化为代码进行执行.例如,我们可以使用以下yaml文件描述多个步骤并且需要关联的接口: api ...

  8. 分布式的一致性(分布式事物)-------2PC详述

    英文名:Two Phase Commit(2PC) 算法目的:实现分布式事物 算法概述: 有两类节点: -----协调者 -----事务参与者 流程阶段: -----请求阶段 -----提交阶段 算法 ...

  9. 树莓派安装配置Nginx+PHP7+MariaDB

    原文地址:http://blog.sina.com.cn/s/blog_150f554f50102yhra.html 一.安装 Nginx 和 PHP7 1.安装Nginx sudo apt inst ...

  10. 详解DLX及其应用

    什么是DLX? 让我们看看百度百科上的解释:在 计算机科学 中, Dancing Links ,舞蹈链, 也叫 DLX, 是由 Donald Knuth 提出的数据结构,目的是快速实现他的 X算法.X ...