JavaScript 程序检查两个数字是否为彼此的位旋转
问题陈述 - 我们给出了两个整数,需要检查这两个数字是否为彼此的位旋转。
在 JavaScript 中,每个整数都是一个 32 位二进制数,它是 0 和 1 的表示形式。在这里,我们需要检查是否旋转第一个数字的 32 位字符串;我们是否可以从第一个数字的总共 32 次旋转中获得第二个数字的 32 位字符串。
使用 ToString() 方法检查两个数字是否为彼此的位旋转
toString() 方法用于将整数转换为 32 位二进制数字字符串。之后,我们可以向二进制字符串添加前导零,使其长度为 32 位。接下来,我们可以将数字的二进制字符串与其自身连接起来,并检查第二个数字的二进制字符串是否作为合并字符串的子字符串存在。
语法
用户可以按照以下语法来检查两个数字在连接字符串后是否为彼此的位旋转。
let num1BinaryDouble = num1Binary + num1Binary; let isBitRotation = num1BinaryDouble.includes(num2Binary)
算法
步骤 1 - 使用 toString() 方法并将其参数传递为 2,将两个数字转换为二进制字符串。
步骤 2 - 接下来,我们需要将两个字符串的大小都设置为 32 位。因此,在两个二进制字符串中添加前导零。
步骤 3 - 将 num1 的二进制字符串与其自身合并。
步骤 4 - 检查合并字符串是否包含 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 循环找到第一个数字的所有位旋转,并将其与第二个数字进行匹配。