作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/number-of-1-bits/

Total Accepted: 88721 Total Submissions: 236174 Difficulty: Easy

题目描述

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

Example 1:

  1. Input: 11
  2. Output: 3
  3. Explanation: Integer 11 has binary representation 00000000000000000000000000001011

Example 2:

  1. Input: 128
  2. Output: 1
  3. Explanation: Integer 128 has binary representation 00000000000000000000000010000000

题目大意

统计一个数字的二进制中有多少个1.

解题方法

右移32次

其实就是不断地右移。判断最后一位总共出现了多少次1.

刚开始没有AC的原因是java是没有无符号整数的,也就是说int的最高位是1的话会认为是负数,所以普通右移并且判断n==0会超时,判断n>0的话直接不运行。

还好java提供了不保持符号位的>>>。

  1. >>是符号位保持不变的右移。

用左移的方法好像不用考虑这么多。

本来是想用n%2==1来判断是否最后一位是1的,效率太差,用n&1的方式可以加快最后一位计算时的速率。

  1. public class Solution {
  2. // you need to treat n as an unsigned value
  3. public int hammingWeight(int n) {
  4. int answer=0;
  5. while(n!=0){
  6. answer+=n&1;
  7. n>>>=1;
  8. }
  9. return answer;
  10. }
  11. }

AC:2ms

计算末尾的1的个数

LeetCode给出的另一种巧妙解法。

当n&(n-1)!=0时说明n中至少包含一个1.

通过不停的n=n&(n-1)的方法可以将最后的一个1消掉。

  1. public int hammingWeight(int n) {
  2. int sum = 0;
  3. while (n != 0) {
  4. sum++;
  5. n &= (n - 1);
  6. }
  7. return sum;
  8. }

python代码如下:

  1. class Solution(object):
  2. def hammingWeight(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. res = 0
  8. while n:
  9. res += 1
  10. n &= n - 1
  11. return res

转成二进制统计1的个数

代码如下。

  1. class Solution(object):
  2. def hammingWeight(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. return bin(n).count("1")

使用mask

和原本的数字移动的方式没有太大区别,只不过用了个变量来看每位是不是1.

  1. class Solution(object):
  2. def hammingWeight(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. mask = 1 << 32
  8. res = 0
  9. while mask:
  10. if n & mask:
  11. res += 1
  12. mask >>= 1
  13. return res

日期

2016/5/1 14:31:33
2018 年 11 月 20 日 —— 真是一个好天气

【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...

  2. 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...

  3. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  6. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  9. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

随机推荐

  1. open 函数小结

    umask 掩码 open 函数的时候需要注意,掩码去反之后和设置的值想与,得到真正的值. 可以在命令行 使用umask 来查询 umask 000 设置掩码

  2. HDC2021技术分论坛:异构组网如何解决共享资源冲突?

    作者:lijie,HarmonyOS软总线领域专家 相信大家对HarmonyOS的"超级终端"比较熟悉了.那么,您知道超级终端场景下的多种设备在不同环境下是如何组成一个网络的吗?这 ...

  3. 多线程高级篇1 — JUC — 只弄到处理高并发集合问题

    1.线程池 1.1).什么是线程池? 池( pool ),就是一个容器,所以线程池就是把多个线程对象放到一个容器中 1.2).如何创建线程池? 先来了解几个常识 Executor -- 这是一个接口( ...

  4. day9 文件处理

    day09 文件处理 一.注册与登录功能 username = input('请输入您的密码:').strip() password = input('请输入您的密码:').strip() f = o ...

  5. 【Reverse】每日必逆0x01

    附件:https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 64位ELF文件,无壳 IDApro处 ...

  6. Insert into select语句引发的生产事故

    前言   Insert into select请慎用.这天xxx接到一个需求,需要将表A的数据迁移到表B中去做一个备份.本想通过程序先查询查出来然后批量插入.但xxx觉得这样有点慢,需要耗费大量的网络 ...

  7. 如何用shell脚本分析网站日志统计PV、404、500等数据

    以下shell脚本能统计出网站的总访问量,以及404,500出现的次数.统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击.还可以根据查看500出现的次数,进而判断网站 ...

  8. java客户端的elasticSearch索引库的相关操作

    package com.hope.es;import org.elasticsearch.client.transport.TransportClient;import org.elasticsear ...

  9. Apache Hudi 与 Hive 集成手册

    1. Hudi表对应的Hive外部表介绍 Hudi源表对应一份HDFS数据,可以通过Spark,Flink 组件或者Hudi客户端将Hudi表的数据映射为Hive外部表,基于该外部表, Hive可以方 ...

  10. 什么是API?

    一.简介 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序,与开发人员基于某软件或硬件得以访问一组例程的能力,而又 ...