Topic

  • Bit Manipulation

Description

https://leetcode.com/problems/number-of-1-bits/

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

Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation 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?

Example 1:

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

Example 2:

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

Example 3:

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

Analysis

方法一:

  • An Integer in Java has 32 bits, e.g. 00101000011110010100001000011010.
  • To count the 1s in the Integer representation we put the input int n in bit AND with 1 (that is represented as 00000000000000000000000000000001, and if this operation result is 1, that means that the last bit of the input integer is 1. Thus we add it to the 1s count.ones = ones + (n & 1);
  • Then we shift the input Integer by one on the right, to check for the next bit.n = n>>>1;
  • We keep doing this until the input Integer is 0.

方法二:

n & (n - 1) drops the lowest set bit. It’s a neat little bit trick.

Let’s use n = 00101100 as an example. This binary representation has three 1s.

  • If n = 00101100, then n - 1 = 00101011, so n & (n - 1) = 00101100 & 00101011 = 00101000. Count = 1.
  • If n = 00101000, then n - 1 = 00100111, so n & (n - 1) = 00101000 & 00100111 = 00100000. Count = 2.
  • If n = 00100000, then n - 1 = 00011111, so n & (n - 1) = 00100000 & 00011111 = 00000000. Count = 3.

n is now zero, so the while loop ends, and the final count (the numbers of set bits) is returned.

Submission

public class NumberOfOneBits {// 方法一:public int hammingWeight1(int n) {int result = 0;while (n != 0) {// if((n & 1) == 1) result++;result += (n & 1);n >>>= 1;}return result;}// 方法二:int hammingWeight2(int n) {int count = 0;while (n != 0) {n &= (n - 1);count++;}return count;}
}

Test

import static org.junit.Assert.*;import org.junit.Test;public class NumberOfOneBitsTest {@Testpublic void test() {NumberOfOneBits obj = new NumberOfOneBits();assertEquals(3, obj.hammingWeight1(Integer.parseInt("00000000000000000000000000001011", 2)));assertEquals(1, obj.hammingWeight1(Integer.parseInt("00000000000000000000000010000000", 2)));assertEquals(31, obj.hammingWeight1(-3));//Integer.parseInt("11111111111111111111111111111101", 2) 抛错assertEquals(3, obj.hammingWeight2(Integer.parseInt("00000000000000000000000000001011", 2)));assertEquals(1, obj.hammingWeight2(Integer.parseInt("00000000000000000000000010000000", 2)));assertEquals(31, obj.hammingWeight2(-3));}
}

LeetCode - Easy - 191. Number of 1 Bits相关推荐

  1. 【LeetCode】191. Number of 1 Bits

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

  2. [勇者闯LeetCode] 191. Number of 1 Bits

    [勇者闯LeetCode] 191. Number of 1 Bits Description Write a function that takes an unsigned integer and ...

  3. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  4. leetcode python3 简单题191. Number of 1 Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...

  5. 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. 【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

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

  7. Leet Code OJ 191. 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 OJ 之 Number of 1 Bits (二进制位1的个数)

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

  9. 191. Number of 1 Bits

最新文章

  1. LeetCode上稀缺的四道shell编程题解析
  2. nginx常用功能介绍
  3. Caffe中的卷积实现
  4. Centos7 下 zabbix服务安装与部署,linux监控服务
  5. 《ArcGIS Runtime SDK for Android开发笔记》
  6. 重塑营销场景,用友优普助宁波力劲销售管理精细化
  7. 调用接口登录禅道_干货,调用api获取禅道需求列表等
  8. 引入其他字体库 和 字体样式设置
  9. nestjs连接mysql数据库的方法与使用
  10. 【classic】MMD镜头+动作打包下载.zip
  11. ArcGis 地理配准注意事项
  12. Delphi为什么都不用了?公司CTO不向老板推荐使用Delphi的13 个真实原因
  13. php 手写签批 手机办公_好签原笔迹手写签批SDK
  14. SpringBoot使用Jib将应用快速打包成Docker镜像
  15. 第1107期AI100_机器学习日报(2017-09-29)
  16. python csv文件和xlsx文件混杂时,提取指定列数据并合并
  17. 批量查询快递单号筛选出代收单号
  18. iOS—— 调用高德地图SDK
  19. GIS开发之二维地下管线综合管理系统(Arcgis)第一节 总体介绍
  20. 如何调整计算机屏幕显示的锐度,win10系统调节显示器锐度的解决步骤

热门文章

  1. html仿qq最小化怎么实现,JS仿QQ好友列表展开、收缩功能(第一篇)
  2. xilinx芯片管脚使用限制_修复焊接BGA芯片过程
  3. GetType和typeof的区别
  4. 由浅到深理解ROS URDF教程
  5. php 变量文件间传递,同一文件的两个JS函数之间如何传变量?
  6. 漏洞:Client ReDos From Regex Injection
  7. int与byte转换(四字节)
  8. 加载中_GIS地图在项目中的加载显示
  9. 【机器学习】 - 激活函数与交叉熵Sigmoid, Softmax, binary_crossentropy, categorican_crossentropy区别
  10. 【2018icpc宁夏邀请赛现场赛】【Gym - 102222H】Fight Against Monsters(贪心排序)