博客
关于我
第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/

    你可能感兴趣的文章
    python selenium自动化测试报告
    查看>>
    Python selenium自动化测试框架实战 —— 登录测试案例
    查看>>
    Python Selenium设计模式 —— POM
    查看>>
    Python Serial:如何使用 read 或 readline 函数一次读取多个字符
    查看>>
    Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?
    查看>>
    Python setup.py:数据文件无法复制目录:不存在或不是常规文件
    查看>>
    Python setuptools sdist:仅安装版本化文件
    查看>>
    Python Shell下使用matplotlib
    查看>>
    Python Slice How-to,我知道Python Slice,但我怎么才能使用内置的Slice对象呢?
    查看>>
    python socket分包发送数据
    查看>>
    python socket模块_Python socket模块实现TCP服务端客户端
    查看>>
    Python SOCKS5代理客户端HTTPS
    查看>>
    Python Soc网络分析:通过使用函数迭代列表来计算机会网络
    查看>>
    python转换已转义的字符串
    查看>>
    Python Sphinx自动摘要:成员函数的自动列表
    查看>>
    Python SQL和NoSQL数据库操作实战
    查看>>
    python stdout flush_sys.stdout.flush()方法的用法
    查看>>
    python string 运算
    查看>>
    Python str与bytes之间的转换
    查看>>
    Python subprocess ffmpeg
    查看>>