[191] Number of 1 Bits

19 篇文章 1 订阅
2 篇文章 1 订阅

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.
读题可真是废了大劲了,一开始没懂是什么意思,看了看Hamming weight的wiki,更晕了。后来一搜,原来是给定一个整数(被看做无符号整数),统计这个无符号整数的二进制表示中有多少个1。

2. 解题思路

第一种想法,最简单的想法,将这个int值直接转换为字符串,统计字符串中1的个数,如Code1,这个方法效率不太高。
第二种想法,通过位移操作,每次用过按位与看最后一位是否为1,如果是1,那么就统计加1。这种方法由于有符号数(pretend to be unsigned)的特性坑比较多。
第三种就是比较牛逼的了,一次移动好几位,最好使用32能整除的位数,比如2,4,8,16之类的,这样移动起来比较方便。

3. Code

// Code1:效率很低
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        // 将数字转为二进制字符串
        String s = Integer.toBinaryString(n);
        int result = 0;
        for (int i = 0; i < s.length(); ++i)
        {
            // 如果字符串中有字符1
            if (s.charAt(i) == '1')
            {
                // 结果个数加1
                ++result;
            }
        }
        return result;
    }
}
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        // 第一坑,约定的无符号数并不代表这个数大于1
        /*if (n <= 0)
        {
            return 0;
        }*/
        int result = 0;
        // 第二坑,MIN_VALUE的abs值是0,而本来的设定是移到0时退出,返回结果
        while(Math.abs(n) > 0 || n == Integer.MIN_VALUE)
        {
            if ((n & 1) == 1)
            {
                ++result;
            }
            // 第三坑,既然符号位也算到统计中,需要使用带符号右移的操作
            n >>>= 1;  // 带符号右移一位,符号位跟着一起向右移动
        }
        return result;
    }
}
    // 附上Math.abs(int value)
    public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
// 最后附一个牛人发明的效率更高的方法
public class Solution {
    // you need to treat n as an unsigned value
    private static final int[] ARR = new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
    public int hammingWeight(int n) {
        int result = 0;
        while(Math.abs(n) > 0 || n == Integer.MIN_VALUE)
        {
            result+=ARR[n & 0xF];
            n >>>= 4; // 一下子移动4位,效率变高,喜欢的话,可以填个2048的,这样计算个3次就够了:)
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值