zoukankan      html  css  js  c++  java
  • 求一个byte数字对应的二进制数字中1的最大连续数

    功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
        
    输入: 一个byte型的数字
        
    输出: 无
         
    返回: 对应的二进制数字中1的最大连续数

    private static void getIntBinary(int n){
    String str = "";
    while (n > 0){
    int t = n & 1;
    str = t + str;
    n = n >> 1;
    }
    System.out.println(str);
    }
    // 时间复杂度为O(n)
    private static void maxBit(String str){
    int max = 0;
    int count = 0;
    for (char c : str.toCharArray()) {
    if(c == '0'){
    count = 0;
    }else {
    count++;
    if(count > max){
    max = count;
    }
    }
    }
    System.out.println(max);

    }
  • 相关阅读:

    快排
    排序算法
    运算符
    二叉树
    递归
    队列
    栈(没写完)
    绘制双坐标轴的图形3-不同的plot类型
    绘制双坐标轴的图形2-不同的plot类型
  • 原文地址:https://www.cnblogs.com/dongma/p/13223373.html
Copyright © 2011-2022 走看看