博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode(191):Number of 1 Bits
阅读量:6857 次
发布时间:2019-06-26

本文共 587 字,大约阅读时间需要 1 分钟。

Number of i Bits:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the ).For example, the 32-bit integer “11” has binary representation 00000000000000000000000000001011, so the function should return 3.

题意:求出一个整数2进制表示时1的个数。

思路:位操作,一位一位右移,然后判断最后一位是否为1。

代码:

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;    }}

转载于:https://www.cnblogs.com/Lewisr/p/5245533.html

你可能感兴趣的文章
JVM系列二:GC策略&内存申请、对象衰老
查看>>
MySQL 数据库备份策略:全备与增量备份
查看>>
Springboot的热部署
查看>>
Thinking in UML-1-为什么需要UML
查看>>
vs编译obj给delphi用
查看>>
过游戏保护NP或TP的几种方法和思路
查看>>
equals和hashcode为什么要一起重写
查看>>
模态与非模态对话框的问题
查看>>
httpclient 备注 控制连接时间及多线程错误
查看>>
地对地导弹地对地导弹地对地导弹
查看>>
浏览器根对象window之performance
查看>>
让div 充满整个body
查看>>
常用排序算法
查看>>
程序员保持快乐活跃的6个好习惯(转)
查看>>
找工作的一些感悟——前端小菜的成长
查看>>
jSON Call can throw but it is not marked with try
查看>>
基于bootstrap的jQuery多级列表树插件 treeview
查看>>
node06
查看>>
笔试题[转]
查看>>
图片轮换
查看>>