博客
关于我
第28题:求整数的二进制表示中1的个数
阅读量:529 次
发布时间:2019-03-08

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

?????????????????????????????????1??????????????????????

????

??????????????????????????

  • ???????????1????
  • ?????????????????0?
  • ???????????????1????????????
  • ????????????????1????
  • ????

    package test028;public class Test028 {    public static int getNumOfOne(int n) {        if (n < 0) {            return -1; // ??????????????        }        int count = 0;        while (n != 0) {            if ((n & 1) == 1) {                count++;            }            n >>= 1;        }        return count;    }    public static void main(String[] args) {        System.out.println("11???????? " + getNumOfOne(11) + " ?1?");    }}

    ????

  • ????????????????????-1????????????????????
  • ???????int count = 0; ????1????
  • ?????while (n != 0)????n??0???????
  • ??????if ((n & 1) == 1)???n????????1????????????
  • ?????n >>= 1; ?n?????????????
  • ?????System.out.println("11???????? " + getNumOfOne(11) + " ?1?"); ?????????????
  • ??????????????????????1??????????O(log n)???????????????

    转载地址:http://hbciz.baihongyu.com/

    你可能感兴趣的文章
    php aes sha1解密,PHP AES加密/解密
    查看>>
    php csv 导出
    查看>>
    PHP imap 远程命令执行漏洞复现(CVE-2018-19518)
    查看>>
    php include和require
    查看>>
    ref 和out 区别
    查看>>
    php JS 导出表格特殊处理
    查看>>
    php json dom解析
    查看>>
    ReentrantReadWriteLock读写锁解析
    查看>>
    php laravel请求处理管道(装饰者模式)
    查看>>
    PHP mongoDB 操作
    查看>>
    ReentrantLock读写锁
    查看>>
    php mysql procedure获取多个结果集
    查看>>
    php mysql query 行数,PHP和MySQL:返回的行数
    查看>>
    PHP mysql_real_escape_string() 函数防SQL注入
    查看>>
    php mysql优化方法_MySQL优化常用方法
    查看>>
    PHP OAuth 2.0 Server
    查看>>
    php odbc驱动,php常用ODBC函数集(详细)
    查看>>
    php openssl aes ecb,php openssl_encrypt AES-128-ECB iOS
    查看>>
    php paypal rest api,PayPal REST API指定网络配置文件PHP
    查看>>
    php pcntl 多进程学习
    查看>>