LeetCode-191

LeetCode算法题191:位1的个数

前言

第一次尝试在算法题中使用与进行(&)位运算,一般多次使用过右移>>,左移<<,异或^这三种。
简单说下与运算(&)原理:1 1为1 其余为0。简单来说只有两个数比较都为1的情况才会为1,其他情况为0。
需要注意的是这题仅仅使用>>是不够的,因为>>仅仅只能满足非负数情况,如果为负数,最高位符号位为1。
原因:>>是舍弃最低位,高位使用符号位补充,这种情况使用>>会发生死循环,舍弃低位后补充符号位1,导致超时。需要使用无符号右移>>>让用0补充。
参考:负雪明烛详解位运算191

方法1-循环遍历

死循环代码

//leetcode submit region begin(Prohibit modification and deletion)
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {

        int count =0;
        while (n != 0) {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

在知道原理后自然就能写出正确的代码,只需要将>>改为>>>即可。(顺便贴一下吧)

正确解法1

//leetcode submit region begin(Prohibit modification and deletion)
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {

        int count =0;
        while (n != 0) {
            count += n & 1;
            n >>>= 1;
        }
        return count;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

方法2-去尾1

在了解过上面方法后,我们可以有更优解的方法,通过删除二进制末尾的1的思路下进行。
原理是通过n&(n-1)让n自动消去最后一个1来计数,具体实现原理还是推荐看负雪明烛详解位运算191
时间复杂度(logn),空间复杂度(1)


//leetcode submit region begin(Prohibit modification and deletion)
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
		//计数器
        int count = 0;
        while(n!=0){
            n &=n-1;
            count++;
        }
        return count;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值