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

    你可能感兴趣的文章
    PLC、DCS、SCADA的选型
    查看>>
    PLC中的电子凸轮的简单介绍
    查看>>
    PLC发展详解-ChatGPT4o作答+匹尔西
    查看>>
    PLC探针有什么用
    查看>>
    PLC接线详解
    查看>>
    PLC数组的使用(西门子)
    查看>>
    Quarzt定时调度任务
    查看>>
    PLC结构体(西门子)
    查看>>
    PLC编程语言ST文本语法的常用数据类型及变量
    查看>>
    PLC通讯方式
    查看>>
    Please install 'webpack-cli' in addition to webpack itself to use the CLI
    查看>>
    Ploly Dash,更新一个Dash应用程序JJJA上的实时人物
    查看>>
    Ploly烛台的定制颜色
    查看>>
    Ploly:如何在Excel中嵌入完全交互的Ploly图形?
    查看>>
    plotloss记录
    查看>>
    Plotly (Python) 子图:填充构面和共享图例
    查看>>
    Plotly 中的行悬停文本
    查看>>
    Plotly 停用 x 轴排序
    查看>>
    Plotly 域变量解释(多图)
    查看>>
    Plotly 绘制表面 3D 未显示
    查看>>