【Java笔试强训】day11编程题

编程题

最近公共祖先

public int getLCA(int a, int b) {
          
   
        // write code here
        while (a != b) {
          
   
            if (a > b) {
          
   
                a = a / 2;
            } else {
          
   
                b = b / 2;
            }
        }
        return a;
    }

求最大连续bit数

public static void main(String[] args) {
          
   
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
          
   
            int n = sc.nextInt();
            int count = 0;
            int modCount = 0;
            while (n != 0) {
          
   
                if ((n & 1) == 1) {
          
   
                    count++;
                    modCount = Math.max(count, modCount);
                } else {
          
   
                    count = 0;
                }
                n >>= 1;
            }
            System.out.println(modCount);
        }
    }
经验分享 程序员 微信小程序 职场和发展