📌  相关文章
📜  检查给定数字是否仅包含“01”和“10”作为其二进制表示中的子字符串

📅  最后修改于: 2021-09-07 03:26:03             🧑  作者: Mango

给定一个数字N ,任务是检查数字N的二进制表示是否只有“01”“10”作为子串。如果发现是真的,则打印“是” 。否则,打印“否”
例子:

方法:可以使用以下观察来解决给定的问题:

  • 由于子字符串必须包含“01”“10” 。因此,二进制表示交替包含10
  • 对于交替包含01 的二进制表示,可以观察到以下模式:
  • 上面的模式是2 * x(4 * x + 1) 的形式,使得系列的最后一项是x

从上面的观察来看,想法是使用上述模式生成给定的系列并检查给定的数字是否存在于模式中。如果发现是真的,则打印“是” 。否则,打印“否”
下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
vector Ans;
 
// Function to generate all numbers
// having "01" and "10" as a substring
void populateNumber()
{
    // Insert 2 and 5
    Ans.push_back(2);
    Ans.push_back(5);
 
    long long int x = 5;
 
    // Iterate till x is 10 ^ 15
    while (x < 1000000000001) {
 
        // Multiply x by 2
        x *= 2;
        Ans.push_back(x);
 
        // Update x as x*2  + 1
        x = x * 2 + 1;
 
        Ans.push_back(x);
    }
}
 
// Function to check if binary representation
// of N contains only "01" and "10" as substring
string checkString(long long int N)
{
    // Function Call to generate all
    // such numbers
    populateNumber();
 
    // Check if a number N
    // exists in Ans[] or not
    for (auto& it : Ans) {
 
        // If the number exists
        if (it == N) {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
{
    long long int N = 5;
 
    // Function Call
    cout << checkString(N);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG
{
  static int N = 200000;
 
  static long Ans[] = new long [200000];
  static int index = 0;
 
  // Function to generate all numbers
  // having "01" and "10" as a substring
  static void populateNumber()
  {
    // Insert 2 and 5
    Ans[index++] = (2);
    Ans[index++] = (5);
    long x = 5;
    long inf = 1000000000001L;
 
    // Iterate till x is 10 ^ 15
    while (x < inf)
    {
 
      // Multiply x by 2
      x *= 2;
      Ans[index++] = (x);
 
      // Update x as x*2  + 1
      x = x * 2 + 1;
 
      Ans[index++] = (x);
    }
  }
  // Function to check if binary representation
  // of N contains only "01" and "10" as substring
  static void checkString(int N)
  {
    // Function Call to generate all
    // such numbers
    populateNumber();
 
    // Check if a number N
    // exists in Ans[] or not
    for (int i = 0; i < index; i++)
    {
 
      // If the number exists
      if (Ans[i] == N)
      {
        System.out.println("YES");
        return;
      }
    }
    System.out.println("NO");
 
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    N = 5;
    checkString(N);
  }
}
 
// This code is contributed by amreshkumar3.


Python3
# Python3 program to implement
# the above approach
Ans = []
 
# Function to generate all numbers
# having "01" and "10" as a substring
def populateNumber() :
     
    # Insert 2 and 5
    Ans.append(2)
    Ans.append(5)
    x = 5
 
    # Iterate till x is 10 ^ 15
    while (x < 1000000000001) :
 
        # Multiply x by 2
        x *= 2
        Ans.append(x)
 
        # Update x as x*2  + 1
        x = x * 2 + 1
        Ans.append(x)
 
# Function to check if binary representation
# of N contains only "01" and "10" as substring
def checkString(N) :
     
    # Function Call to generate all
    # such numbers
    populateNumber()
 
    # Check if a number N
    # exists in Ans[] or not
    for it in Ans :
 
        # If the number exists
        if (it == N) :
            return "Yes"
 
    # Otherwise
    return "No"
 
# Driver Code
N = 5
 
# Function Call
print(checkString(N))
 
# This code is contributed by sanjoy_62


C#
// C# Program to implement
// the above approach
using System;
class GFG
{
  static int N = 200000;
  static long []Ans = new long [200000];
  static int index = 0;
 
  // Function to generate all numbers
  // having "01" and "10" as a substring
  static void populateNumber()
  {
     
    // Insert 2 and 5
    Ans[index++] = (2);
    Ans[index++] = (5);
    long x = 5;
    long inf = 1000000000001L;
 
    // Iterate till x is 10 ^ 15
    while (x < inf)
    {
 
      // Multiply x by 2
      x *= 2;
      Ans[index++] = (x);
 
      // Update x as x*2  + 1
      x = x * 2 + 1;
      Ans[index++] = (x);
    }
  }
   
  // Function to check if binary representation
  // of N contains only "01" and "10" as substring
  static void checkString(int N)
  {
     
    // Function Call to generate all
    // such numbers
    populateNumber();
 
    // Check if a number N
    // exists in Ans[] or not
    for (int i = 0; i < index; i++)
    {
 
      // If the number exists
      if (Ans[i] == N)
      {
        Console.WriteLine("YES");
        return;
      }
    }
    Console.WriteLine("NO");
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    N = 5;
    checkString(N);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
Yes

时间复杂度: O(1)
辅助空间: O(1)