📜  在数字的二进制表示中找到长度> = n的连续1

📅  最后修改于: 2021-05-25 01:24:08             🧑  作者: Mango

给定两个整数xn ,任务是搜索长度大于或等于n的第一个连续的1s流(以x的32位二进制表示),并返回其位置。如果不存在这样的字符串,则返回-1。

例子:

方法:使用按位运算来计算编号。的数字中的前导零,然后使用它找到我们需要从中开始搜索连续的1的位置。跳过搜索前导零。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the
// number of leading zeros
int countLeadingZeros(int x)
{
    unsigned y;
    int n;
    n = 32;
    y = x >> 16;
    if (y != 0) {
        n = n - 16;
        x = y;
    }
    y = x >> 8;
    if (y != 0) {
        n = n - 8;
        x = y;
    }
    y = x >> 4;
    if (y != 0) {
        n = n - 4;
        x = y;
    }
    y = x >> 2;
    if (y != 0) {
        n = n - 2;
        x = y;
    }
    y = x >> 1;
    if (y != 0)
        return n - 2;
    return n - x;
}
  
// Function to find the string
// of n consecutive 1's
int FindStringof1s(unsigned x, int n)
{
    int k, p;
  
    // Initialize position to return.
    p = 0;
    while (x != 0) {
  
        // Skip leading 0's
        k = countLeadingZeros(x);
        x = x << k;
  
        // Set position after leading 0's
        p = p + k;
  
        // Count first group of 1's.
        k = countLeadingZeros(~x);
  
        // If length of consecutive 1's
        // is greater than or equal to n
        if (k >= n)
            return p + 1;
  
        // Not enough 1's
        // skip over to next group
        x = x << k;
  
        // Update the position
        p = p + k;
    }
  
    // if no string is found
    return -1;
}
  
// Driver code
int main()
{
    int x = 35;
    int n = 2;
    cout << FindStringof1s(x, n);
}


Java
// Java  implementation of above approach
  
import java.io.*;
  
class GFG {
// Function to count the
// number of leading zeros
static int countLeadingZeros(int x)
{
    int y;
    int n;
    n = 32;
    y = x >> 16;
    if (y != 0) {
        n = n - 16;
        x = y;
    }
    y = x >> 8;
    if (y != 0) {
        n = n - 8;
        x = y;
    }
    y = x >> 4;
    if (y != 0) {
        n = n - 4;
        x = y;
    }
    y = x >> 2;
    if (y != 0) {
        n = n - 2;
        x = y;
    }
    y = x >> 1;
    if (y != 0)
        return n - 2;
    return n - x;
}
  
// Function to find the string
// of n consecutive 1's
static int FindStringof1s(int x, int n)
{
    int k, p;
  
    // Initialize position to return.
    p = 0;
    while (x != 0) {
  
        // Skip leading 0's
        k = countLeadingZeros(x);
        x = x << k;
  
        // Set position after leading 0's
        p = p + k;
  
        // Count first group of 1's.
        k = countLeadingZeros(~x);
  
        // If length of consecutive 1's
        // is greater than or equal to n
        if (k >= n)
            return p + 1;
  
        // Not enough 1's
        // skip over to next group
        x = x << k;
  
        // Update the position
        p = p + k;
    }
  
    // if no string is found
    return -1;
}
  
// Driver code
      
    public static void main (String[] args) {
  
    int x = 35;
    int n = 2;
    System.out.println(FindStringof1s(x, n));
    }
}


C#
// C# implementation of above approach
  
using System;
  
public class GFG{
      
// Function to count the
// number of leading zeros
static int countLeadingZeros(int x)
{
    int y;
    int n;
    n = 32;
    y = x >> 16;
    if (y != 0) {
        n = n - 16;
        x = y;
    }
    y = x >> 8;
    if (y != 0) {
        n = n - 8;
        x = y;
    }
    y = x >> 4;
    if (y != 0) {
        n = n - 4;
        x = y;
    }
    y = x >> 2;
    if (y != 0) {
        n = n - 2;
        x = y;
    }
    y = x >> 1;
    if (y != 0)
        return n - 2;
    return n - x;
}
  
// Function to find the string
// of n consecutive 1's
static int FindStringof1s(int  x, int n)
{
    int k, p;
  
    // Initialize position to return.
    p = 0;
    while (x != 0) {
  
        // Skip leading 0's
        k = countLeadingZeros(x);
        x = x << k;
  
        // Set position after leading 0's
        p = p + k;
  
        // Count first group of 1's.
        k = countLeadingZeros(~x);
  
        // If length of consecutive 1's
        // is greater than or equal to n
        if (k >= n)
            return p + 1;
  
        // Not enough 1's
        // skip over to next group
        x = x << k;
  
        // Update the position
        p = p + k;
    }
  
    // if no string is found
    return -1;
}
  
// Driver code
      
    static public void Main (){
    int x = 35;
    int n = 2;
    Console.WriteLine (FindStringof1s(x, n));
    }
}


输出:
31

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。