📌  相关文章
📜  二进制表示形式中最长连续1s的长度

📅  最后修改于: 2021-05-04 19:33:14             🧑  作者: Mango

给定数字n,以二进制表示形式找到最长连续1的长度。
例子 :

Input : n = 14
Output : 3
The binary representation of 14 is 1110.

Input : n = 222
Output : 4
The binary representation of 222 is 11011110.

天真的方法:一种简单的方法是简单地循环遍历这些位,并跟踪连续设置的位的数量以及该值已达到的最大值。在这种方法中,我们需要将其转换为二进制(base-2)表示形式,然后查找并打印结果。

使用位魔术:这个想法基于以下概念:如果我们将位序列与自身的移位版本进行“与”运算,我们将有效地从每个连续的1序列中删除尾随的1。

11101111   (x)
    & 11011110   (x << 1)
    ----------
      11001110   (x & (x << 1)) 
        ^    ^
        |    |
   trailing 1 removed

因此,操作x =(x&(x << 1))在x的二进制表示形式中将每个1s序列的长度减少1。如果我们继续循环执行此操作,则最终x =0。达到0所需的迭代次数实际上是最长连续序列1s的长度。

C++
// C++ program to find length of the longest
// consecutive 1s in binary reprsentation of
// a number.
#include
using namespace std;
 
int maxConsecutiveOnes(int x)
{
    // Initialize result
    int count = 0;
 
    // Count the number of iterations to
    // reach x = 0.
    while (x!=0)
    {
        // This operation reduces length
        // of every sequence of 1s by one.
        x = (x & (x << 1));
 
        count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    cout << maxConsecutiveOnes(14) << endl;
    cout << maxConsecutiveOnes(222) << endl;
    return 0;
}


Java
// Java program to find length of the longest
// consecutive 1s in binary reprsentation of
// a number.
class MaxConsecutiveOnes
{
    private static int maxConsecutiveOnes(int x)
    {
        // Initialize result
        int count = 0;
 
        // Count the number of iterations to
        // reach x = 0.
        while (x!=0)
        {
            // This operation reduces length
            // of every sequence of 1s by one.
            x = (x & (x << 1));
 
            count++;
        }
 
        return count;
    }
 
    // Driver code
    public static void main(String strings[])
    {
        System.out.println(maxConsecutiveOnes(14));
        System.out.println(maxConsecutiveOnes(222));
    }
}


Python3
# Python program to find
# length of the longest
# consecutive 1s in
# binary reprsentation of
# a number.
 
def maxConsecutiveOnes(x):
 
    # Initialize result
    count = 0
  
    # Count the number of iterations to
    # reach x = 0.
    while (x!=0):
     
        # This operation reduces length
        # of every sequence of 1s by one.
        x = (x & (x << 1))
  
        count=count+1
     
    return count
 
# Driver code
 
print(maxConsecutiveOnes(14))
print(maxConsecutiveOnes(222))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find length of the
// longest consecutive 1s in binary
// reprsentation of a number.
using System;
 
class GFG {
     
    // Function to find length of the
    // longest consecutive 1s in binary
    // reprsentation of a number
    private static int maxConsecutiveOnes(int x)
    {
         
        // Initialize result
        int count = 0;
 
        // Count the number of iterations
        // to reach x = 0.
        while (x != 0)
        {
             
            // This operation reduces length
            // of every sequence of 1s by one.
            x = (x & (x << 1));
 
            count++;
        }
 
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        Console.WriteLine(maxConsecutiveOnes(14));
        Console.Write(maxConsecutiveOnes(222));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出 :

3
4

https://youtu.be/X06Vml