📜  检查数字是否有其他模式的位| Set-2 O(1)方法

📅  最后修改于: 2021-04-24 19:46:53             🧑  作者: Mango

给定正整数n 。问题是检查此整数是否在其二进制表示形式中具有备用模式。这里备用模式的装置,所述设置和取消位在发生在替代顺序。例如-5具有替代图案,即101。
如果有其他图案,则打印“是”,否则为“否”。

注意: 0

例子 :

Input : 10
Output : Yes
(10)10 = (1010)2, has an alternate pattern.

Input : 12
Output : No
(12)10 = (1100)2, does not have an alternate pattern.

简单方法:本文已讨论了时间复杂度为O(n)的问题。

高效方法:以下是步骤:

  1. 计算num = n ^(n >> 1)。如果n具有备用模式,则n ^(n >> 1)运算将产生仅具有设置位的数字。 ‘^’是按位XOR运算。
  2. 检查是否已设置num中的所有位。请参阅这篇文章。
C++
// C++ implementation to check if a number 
// has bits in alternate pattern
#include 
  
using namespace std;
  
// function to check if all the bits are set or not
// in the binary representation of 'n'
bool allBitsAreSet(unsigned int n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;
      
    // else all bits are not set
    return false;
}
  
// function to check if a number 
// has bits in alternate pattern
bool bitsAreInAltOrder(unsigned int n)
{
    unsigned int num = n ^ (n >> 1);
      
    // to check if all bits are set 
    // in 'num'
    return allBitsAreSet(num);        
}
  
// Driver program to test above
int main()
{
    unsigned int n = 10;
      
    if (bitsAreInAltOrder(n))
        cout << "Yes";
    else
        cout << "No";
          
    return 0;        
}


Java
// Java implementation to check if a 
// number has bits in alternate pattern
class AlternateSetBits
{
    // function to check if all the bits 
    // are set or not in the binary 
    // representation of 'n'
    static boolean allBitsAreSet(int n)
    {
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
            
        // else all bits are not set
        return false;
    }
        
    // function to check if a number 
    // has bits in alternate pattern
    static boolean bitsAreInAltOrder(int n)
    {
        int num = n ^ (n >>> 1);
            
        // to check if all bits are set 
        // in 'num'
        return allBitsAreSet(num);        
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
          
        if (bitsAreInAltOrder(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
/* This code is contributed by Danish Kaleem */


Python3
# Python implementation to check if a number 
# has bits in alternate pattern
  
# function to check if all the bits are set or not
# in the binary representation of 'n'
def allBitsAreSet(n):
      
    # if true, then all bits are set
    if (((n + 1) & n) == 0):
        return True;
      
    # else all bits are not set
    return False;
  
  
# function to check if a number 
# has bits in alternate pattern
def bitsAreInAltOrder(n):
    num = n ^ (n >> 1);
      
    # to check if all bits are set 
    # in 'num'
    return allBitsAreSet(num);     
  
  
# Driver code
n = 10;
  
if (bitsAreInAltOrder(n)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by PrinciRaj1992


C#
// C# implementation to check if a
// number has bits in alternate pattern
using System;
  
class GFG {
  
    // function to check if all the bits
    // are set or not in the binary
    // representation of 'n'
    static bool allBitsAreSet(int n)
    {
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
  
        // else all bits are not set
        return false;
    }
  
    // function to check if a number
    // has bits in alternate pattern
    static bool bitsAreInAltOrder(int n)
    {
        int num = n ^ (n >> 1);
  
        // to check if all bits are set
        // in 'num'
        return allBitsAreSet(num);
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 10;
  
        if (bitsAreInAltOrder(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
  
}
  
// This code is contributed by Sam007


PHP
> 1);
      
    // to check if all bits 
    // are set in 'num'
    return allBitsAreSet($num); 
}
  
// Driver Code
$n = 10;
  
if (bitsAreInAltOrder($n))
    echo "Yes";
else
    echo "No";
      
// This code is contributed by aj_36
?>


输出 :

Yes