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 迴圈找到第一個數字的所有位旋轉,並將其與第二個數字進行匹配。

更新於:2023年4月24日

170 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.