📌  相关文章
📜  检查两个数字的二进制表示形式是否为字谜

📅  最后修改于: 2021-04-24 18:04:36             🧑  作者: Mango

给定两个数字,您需要检查它们是否以二进制表示形式彼此正确。

例子:

Input : a = 8, b = 4 
Output : Yes
Binary representations of both
numbers have same 0s and 1s.

Input : a = 4, b = 5
Output : No

简单方法:

  1. 使用简单的十进制到二进制表示技术查找“ a”和“ b”的二进制表示。
  2. 检查两个二进制表示形式是否为字谜
C/C++
// A simple C++ program to check if binary
// representations of two numbers are anagram.
#include 
#define ull unsigned long long int
using namespace std;
  
const int SIZE = 8 * sizeof(ull);
  
bool bit_anagram_check(ull a, ull b)
{
    // Find reverse binary representation of a
    // and store it in binary_a[]
    int i = 0, binary_a[SIZE] = { 0 };
    while (a > 0) {
        binary_a[i] = a % 2;
        a /= 2;
        i++;
    }
  
    // Find reverse binary representation of b
    // and store it in binary_a[]
    int j = 0, binary_b[SIZE] = { 0 };
    while (b > 0) {
        binary_b[j] = b % 2;
        b /= 2;
        j++;
    }
  
    // Sort two binary representations
    sort(binary_a, binary_a + SIZE);
    sort(binary_b, binary_b + SIZE);
  
    // Compare two sorted binary representations
    for (int i = 0; i < SIZE; i++)
        if (binary_a[i] != binary_b[i]) 
            return false;
  
    return true;
}
  
// Driver code
int main()
{
    ull a = 8, b = 4;
    cout << bit_anagram_check(a, b) << endl;
    return 0;
}


Java
// A simple Java program to check if binary
// representations of two numbers are anagram
import java.io.*;
import java.util.*;
  
class GFG 
{
    public static int SIZE = 8;
      
    // Function to check if binary representation
    // of two numbers are anagram
    static int bit_anagram_check(long a, long b)
    {
        // Find reverse binary representation of a
        // and store it in binary_a[]
        int i = 0;
        long[] binary_a = new long[SIZE];
        Arrays.fill(binary_a, 0);
        while (a > 0) 
        {
            binary_a[i] = a%2;
            a /= 2;
            i++;
        }
   
        // Find reverse binary representation of b
        // and store it in binary_a[]
        int j = 0;
        long[] binary_b = new long[SIZE];
        Arrays.fill(binary_b, 0);
        while (b > 0) 
        {
            binary_b[j] = b%2;
            b /= 2;
            j++;
        }
   
        // Sort two binary representations
        Arrays.sort(binary_a);
        Arrays.sort(binary_b);
   
        // Compare two sorted binary representations
        for (i = 0; i < SIZE; i++)
            if (binary_a[i] != binary_b[i]) 
                return 0;
   
        return 1;
    }
  
    // driver program
    public static void main (String[] args) 
    {
        long a = 8, b = 4;
        System.out.println(bit_anagram_check(a, b));
    }
}
  
// Contributed by Pramod Kumar


C#
// A simple C# program to check if 
// binary representations of two
// numbers are anagram 
using System;
  
class GFG
{
public static int SIZE = 8;
  
// Function to check if binary 
// representation of two numbers
// are anagram 
public static int bit_anagram_check(long a, 
                                    long b)
{
    // Find reverse binary representation 
    // of a and store it in binary_a[] 
    int i = 0;
    long[] binary_a = new long[SIZE];
    Arrays.Fill(binary_a, 0);
    while (a > 0)
    {
        binary_a[i] = a % 2;
        a /= 2;
        i++;
    }
  
    // Find reverse binary representation  
    // of b and store it in binary_a[] 
    int j = 0;
    long[] binary_b = new long[SIZE];
    Arrays.Fill(binary_b, 0);
    while (b > 0)
    {
        binary_b[j] = b % 2;
        b /= 2;
        j++;
    }
  
    // Sort two binary representations 
    Array.Sort(binary_a);
    Array.Sort(binary_b);
  
    // Compare two sorted binary 
    // representations 
    for (i = 0; i < SIZE; i++)
    {
        if (binary_a[i] != binary_b[i])
        {
            return 0;
        }
    }
  
    return 1;
}
  
public static class Arrays
{
public static T[] CopyOf(T[] original, 
                            int newLength)
{
    T[] dest = new T[newLength];
    System.Array.Copy(original, dest, newLength);
    return dest;
}
  
public static T[] CopyOfRange(T[] original, 
                                 int fromIndex, 
                                 int toIndex)
{
    int length = toIndex - fromIndex;
    T[] dest = new T[length];
    System.Array.Copy(original, fromIndex, 
                         dest, 0, length);
    return dest;
}
  
public static void Fill(T[] array, T value)
{
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = value;
    }
}
  
public static void Fill(T[] array, int fromIndex,
                           int toIndex, T value)
{
    for (int i = fromIndex; i < toIndex; i++)
    {
        array[i] = value;
    }
}
}
  
  
// Driver Code 
public static void Main(string[] args)
{
    long a = 8, b = 4;
    Console.WriteLine(bit_anagram_check(a, b));
}
}
  
// This code is contributed by Shrikant13


C/C++
// An efficient C++ program to check if binary
// representations of two numbers are anagram.
#include 
#define ull unsigned long long int
using namespace std;
  
// Returns true if binary representations of
// a and b are anagram.
bool bit_anagram_check(ull a, ull b)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a.
    return (_popcnt64(a) == _popcnt64(b))
}
  
int main()
{
    ull a = 8, b = 4;
    cout << bit_anagram_check(a, b) << endl;
    return 0;
}


Java
–// An efficient Java program to check if binary
// representations of two numbers are anagram
import java.io.*;
  
class GFG 
{
    // Function returns true if binary representations of
    // a and b are anagram
    static boolean bit_anagram_check(long a, long b)
    {
        // Long.bitCount(a) gives number of 1's present
        // in binary representation of a
        return (Long.bitCount(a) == Long.bitCount(b));
    }
      
    // driver program
    public static void main (String[] args) 
    {
        long a = 8, b = 4;
        if(bit_anagram_check(a, b))
            System.out.println("1");
        else
            System.out.println("0");
    }
}
  
// Contributed by Pramod Kumar


C#
// An efficient C# program to check if binary
// representations of two numbers are anagram
using System;
  
class GFG 
{
    // Function returns true if binary representations 
    // of a and b are anagram
    static Boolean bit_anagram_check(long a, long b)
    {
        // Long.bitCount(a) gives number of 1's present
        // in binary representation of a
        return (bitCount(a) == bitCount(b));
    }
    static int bitCount(long n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n &= (n - 1);
        }
        return count;
    } 
      
    // Driver Code
    public static void Main (String[] args) 
    {
        long a = 8, b = 4;
        if(bit_anagram_check(a, b))
            Console.WriteLine("1");
        else
            Console.WriteLine("0");
    }
}
  
// This code is contributed by Rajput-Ji


输出:

1

时间复杂度: O(n log n)
辅助空间: O(1)尽管辅助空间为O(1),但SIZE数组空间仍用于存储每个数字的二进制表示形式。

高效方法:
只需测量两个数字的位表示形式中存在的1的数量,如果它们的位表示形式中存在的1的数量相同,则它们在其位表示形式中是字谜(anagram),否则就不同。

C / C++

// An efficient C++ program to check if binary
// representations of two numbers are anagram.
#include 
#define ull unsigned long long int
using namespace std;
  
// Returns true if binary representations of
// a and b are anagram.
bool bit_anagram_check(ull a, ull b)
{
    // _popcnt64(a) gives number of 1's present
    // in binary representation of a.
    return (_popcnt64(a) == _popcnt64(b))
}
  
int main()
{
    ull a = 8, b = 4;
    cout << bit_anagram_check(a, b) << endl;
    return 0;
}

Java

// An efficient Java program to check if binary
// representations of two numbers are anagram
import java.io.*;
  
class GFG 
{
    // Function returns true if binary representations of
    // a and b are anagram
    static boolean bit_anagram_check(long a, long b)
    {
        // Long.bitCount(a) gives number of 1's present
        // in binary representation of a
        return (Long.bitCount(a) == Long.bitCount(b));
    }
      
    // driver program
    public static void main (String[] args) 
    {
        long a = 8, b = 4;
        if(bit_anagram_check(a, b))
            System.out.println("1");
        else
            System.out.println("0");
    }
}
  
// Contributed by Pramod Kumar

C#

// An efficient C# program to check if binary
// representations of two numbers are anagram
using System;
  
class GFG 
{
    // Function returns true if binary representations 
    // of a and b are anagram
    static Boolean bit_anagram_check(long a, long b)
    {
        // Long.bitCount(a) gives number of 1's present
        // in binary representation of a
        return (bitCount(a) == bitCount(b));
    }
    static int bitCount(long n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n &= (n - 1);
        }
        return count;
    } 
      
    // Driver Code
    public static void Main (String[] args) 
    {
        long a = 8, b = 4;
        if(bit_anagram_check(a, b))
            Console.WriteLine("1");
        else
            Console.WriteLine("0");
    }
}
  
// This code is contributed by Rajput-Ji

输出:

1

请注意,以上代码使用了GCC特定功能。如果我们希望为其他编译器编写代码,则可以使用整数形式的Count设置位。

时间复杂度: O(1)
辅助空间: O(1)没有多余的空间被使用。