JavaScript 检查两个数字是否是彼此的位旋转

JavaScript 检查两个数字是否是彼此的位旋转

问题陈述 - 我们给出两个整数,需要检查这两个数是否是彼此的位旋转。

在JavaScript中,每个整数都是一个32位二进制数,表示为0和1。在这里,我们需要检查如果我们旋转第一个数的32位字符串,我们是否可以通过对第一个数进行32次旋转来获得第二个数的32位字符串。

使用ToString()方法检查两个数字是否是彼此的位旋转

使用toString()方法将整数转换为32位二进制数字符串。然后,我们可以在二进制字符串前面添加前导零,使其成为32位长。接下来,我们可以将数字的二进制字符串与自身连接起来,并检查第二个数字的二进制字符串是否作为合并字符串的子字符串存在。

语法

在连接字符串后,用户可以按照以下语法检查两个数字是否是彼此的位旋转。

let num1BinaryDouble = num1Binary + num1Binary;
let isBitRotation = num1BinaryDouble.includes(num2Binary)

步骤

  • 第一步 − 使用toString()方法,将参数设置为2,将两个数字都转换成二进制字符串。

  • 第二步 − 接下来,我们需要将两个二进制字符串的大小都设置为32位。因此,在两个二进制字符串前面添加前导零。

  • 第三步 − 将num1的二进制字符串与自身合并。

  • 第四步 − 检查合并的字符串是否包含num2的二进制字符串。如果是,则表示两个数字互为位旋转。

示例1

在下面的示例中,checkBitRotations()函数实现了上述算法,以确保两个数字是否互为位旋转。从输出中,用户可以观察到1和2是位旋转的,但1和5不是。

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 1;
      let num2 = 2;
      let num3 = 5;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // append remaining zeros at the start of num1BInary and num2Binary to make it's length 32
         while (num1Binary.length < 32) {
            num1Binary = "0" + num1Binary;
         }
         while (num2Binary.length < 32) {
            num2Binary = "0" + num2Binary;
         }
         // double the string
         let num1BinaryDouble = num1Binary + num1Binary;
         // check if num2Binary is present in num1BinaryDouble
         if (num1BinaryDouble.includes(num2Binary)) {
            return true;
         } else {
            return false;
         }
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>

使用For循环检查两个数字是否是彼此的位旋转

在这种方法中,我们将把数字转换为二进制字符串。之后,我们将使用for循环来获取第一个数字的所有旋转,并将所有旋转与第二个数字进行比较。如果第一个数字的任何一个旋转与第二个数字相匹配,则它们是彼此的位旋转。

语法

用户可以按照下面的语法来匹配第一个数字的所有旋转与第二个数字,并确保它们是彼此的位旋转。

for (let i = 0; i < num1Binary.length; i++) {
   if (num1Binary === num2Binary) {
      return true;
   }
   num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
}

在上述语法中,我们逐个比较第一个数字的旋转与第二个数字,并且如果匹配,则返回 true。

步骤

  • 第 1 步 − 使用 toString() 方法将两个数字都转换为二进制字符串。

  • 第 2 步 − 现在,添加前导零,使它们具有相等的长度。

  • 第 3 步 − 使用 for 循环遍历第一个字符串。

  • 第 4 步 − 如果 num1Binary 与 num2Binary 匹配,返回 true。

  • 第 5 步 − 在 for 循环中,如果第一个数字的当前旋转与第二个数字不匹配,则旋转第一个数字并获取一个新的旋转。

  • 第 6 步 − 继续将下一个旋转与第二个旋转进行匹配,直到找到任何匹配的旋转。如果任何旋转不匹配,返回 false。

示例2

在下面的示例中,我们已经实现了上述算法来检查位旋转。这里,我们逐个获取第一个数字的每个旋转,并将它们与第二个数字进行比较。如果任何旋转匹配,我们将返回 true,用户可以在输出中观察到。

<html>
<body>
   <h3>Checking if <i> two numbers are bit rotations of each other or not </i> in JavaScript</h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById("output");
      let num1 = 122;
      let num2 = 2147483678;
      let num3 = 1;
      function checkBitRotation(num1, num2) {
         let num1Binary = num1.toString(2);
         let num2Binary = num2.toString(2);
         // adding leading zeros to make both numbers of the same length
         while (num1Binary.length < num2Binary.length) {
            num1Binary = "0" + num1Binary;
         }
         // checking num1Binary and num2Binary are rotations of each other using for loop
         for (let i = 0; i < num1Binary.length; i++) {
            if (num1Binary === num2Binary) {
               return true;
            }
            num1Binary = num1Binary[num1Binary.length - 1] + num1Binary.substring(0, num1Binary.length - 1);
         }
         return false;
      }
      output.innerHTML += "The " + num1 + " and " + num2 + " are bit rotations of each other " + checkBitRotation(num1, num2) + "<br>";
      output.innerHTML += "The " + num1 + " and " + num3 + " are bit rotations of each other " + checkBitRotation(num1, num3) + "<br>";
   </script>
</body>
</html>

用户学习了两种不同的方法来检查两个数字是否是彼此的位旋转。第一种方法是将第一个字符串与其自身连接,并检查第二个数字是否存在于子字符串中。第二种方法是使用for循环找到第一个数字的所有位旋转,并与第二个数字进行匹配。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程