透過移除給定字串中存在的十六進位制表示中的字元來修改陣列


我們給定了一個正整數陣列,需要修改陣列的每個元素,方法是從當前元素的十六進位制表示中移除給定字串 'hex' 中的字元。

為了解決這個問題,我們可以將當前數字轉換為十六進位制數。然後,我們可以移除十六進位制字串中與 'hex' 和當前十六進位制字串共有的字元。修改十六進位制字串後,我們可以將其轉換回十進位制。

問題陳述 – 我們給定一個包含正整數的陣列,陣列長度為 N。此外,我們還給定了包含 0 到 9 的數字以及 'A' 到 'F' 字元的字串 'hex'。我們需要將每個陣列值轉換為十六進位制,移除 'hex' 字串中存在的數字十六進位制表示中的字元,然後將其再次轉換回十進位制數。

示例

輸入 – array[] = {92, 90, 75, 129}, hex = '1BC'

輸出 – [5, 90, 4, 8]

解釋

  • 92 的十六進位制表示為 '5C'。由於 'C' 出現在 'hex' 字串中,因此我們必須將其移除。因此,結果字串將為 '5';將其轉換為十進位制後,我們得到 5。

  • 90 的十六進位制值為 '5A'。由於 '5' 或 'A' 不在 'hex' 字串中,因此它將保持不變。

  • 75 的十六進位制表示為 '4B',移除 'B' 後,字串變為 '4'。當我們將 '4' 轉換為十進位制時,我們得到 4。

  • 129 的十六進位制值為 '81'。移除 '1' 後,修改後的十六進位制字串為 '8',其十進位制表示為 8。

輸入 – array[] = {54, 43, 56, 9999, 1234, 32}, hex = "1BCFE234"

輸出 – [6, 0, 8, 112, 13, 0]

解釋

  • 實際數字 -> 十六進位制 -> 修改後的十六進位制 -> 最終十進位制

  • 54 -> 36 -> 6 -> 6

  • 43 -> 2B -> 0 -> 0

  • 56 -> 38 -> 8 -> 8

  • 9999 -> 270F -> 70 -> 112

  • 1234 -> 4D2 -> D -> 13

  • 32 -> 20 -> 0 -> 0

方法 1

在這種方法中,我們將首先將十進位制值轉換為十六進位制值。然後,我們將修改十六進位制值。接下來,我們將修改後的十六進位制值轉換為十進位制,並用結果值替換陣列元素。此外,我們將使用不同的方法從十六進位制字串中移除字元,將十進位制轉換為十六進位制,以及將十六進位制轉換為十進位制數字。

演算法

  • 在 changeArr() 函式中開始遍歷每個字串字元。

  • 使用 decimalToHexa() 函式將陣列值轉換為十六進位制值,並將結果儲存在字串變數 'hexa' 中。

    • 在 decimalToHexa() 函式中,如果數字為 0,則返回 0。

    • 定義陣列 alpha[] 並存儲字元。另外,定義字串 'hexa'。

    • 進行迭代,直到數字大於零。

    • 如果 num % 16 <10 為真,則將 num % 16 附加到字串 'hexa'。否則,將 alpha[num % 16 - 10] 附加到字串 'hexa',因為我們需要為 11 到 16 的值附加字元。

    • 將 num 除以 16。

    • 當 while 迴圈迭代完成後,使用 reverse() 方法反轉字串 'hexa' 並返回其值。

  • 現在,使用 removeChars() 函式移除字串 'hexa' 中的字元,這些字元在 'hexa' 和給定的字串 'hex' 之間是共有的。

    • 在 removeChars() 函式中,定義名為 'charSet' 的集合,並將字串 'hex' 的所有字元插入其中。

    • 現在,遍歷字串 'hexVal'。如果字串 'hexVal' 的當前字元存在於 charset 中,則繼續迭代。否則,將當前字元附加到臨時字串 'res'。

    • 返回字串 'res'。

  • 現在,使用 decimalToHexa() 函式將修改後的十六進位制字串轉換為十進位制。

    • 在 decimalToHexa() 函式中,定義陣列 'mp',其中包含從 10 到 15 的數字。

    • 反轉十六進位制字串。

    • 遍歷十六進位制字串。將字元值乘以 16pos,並將其新增到值 'res' 中。

    • – 將值 'pos' 加 1。

  • 用值 'dec' 替換陣列元素。

示例

#include <bits/stdc++.h>
using namespace std;
string decimalToHexa(int num){
   if (num == 0) {
      return "0";
   }
   // char array to store hexadecimal number
   char alpha[] = {'A', 'B', 'C', 'D', 'E', 'F'};
   string Hexa;
   // Traverse the number until it becomes zero
   while (num > 0) {
      // if the remainder is less than 10, store it as it is
      if (num % 16 < 10) {
          Hexa += to_string(num % 16);
      } else {
          // else store the character
          Hexa += alpha[num % 16 - 10];
      }
      // divide the number by 16
      num /= 16;
   }
   // reverse the string
   reverse(Hexa.begin(), Hexa.end());
   return Hexa;
}
int hexaToDecimal(string modifiedHex) {
   // stores hexadecimal to decimal
   char mp[] = {10, 11, 12, 13, 14, 15};
   // Stores result
   int res = 0;
   int pos = 0;
   // reverse the string
   reverse(modifiedHex.begin(), modifiedHex.end());
   // Traverse the string
   for (char ch : modifiedHex) {
      // If a digit, multiply it with 16^pos
      if (isdigit(ch)) {
          res += ((int)pow(16, pos)) * (ch - '0');
      }
      // If character, multiply it with 16^pos
      else {
          res += ((int)pow(16, pos)) * mp[ch - 'A'];
      }
      // Increment the position
      pos += 1;
   }
   // Return the answer
   return res;
}
string removeChars(string hexaVal, string hex) {
   // set to store the characters of the given string
   set<char> charSet;
   // insert the characters of the given string in the set
   for (char ch : hex) {
      charSet.insert(ch);
   }
   // string to store the final hexadecimal number
   string res = "";
   // traverse the hexadecimal number string
   for (char ch : hexaVal) {
      // if the character is present in the set, then continue
      if (charSet.find(ch) != charSet.end()) {
          continue;
      }
      // else add the character to the final string
      res += ch;
   }
   // return the final string
   return res;
}
// function to modify the array as per the given condition
void changeArr(int array[], int N, string hex) {
   // Traverse the array
   for (int i = 0; i < N; i++) {
      // covnert the number to hexadecimal
      string hexa = decimalToHexa(array[i]);
      // remove the characters from the hexadecimal number string which are present in the given string
      string modifiedHex = removeChars(hexa, hex);
      // change the hexadecimal number to decimal
      int decVal = hexaToDecimal(modifiedHex);
      // replace the number with the new decimal value
      array[i] = decVal;
   }
   cout << "The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is " << endl;
   // Print the modified array
   for (int i = 0; i < N; i++) {
      cout << array[i] << " ";
   }
}
int main() {
   // Given array
   int array[] = {54, 43,56,9999,1234, 32};
   int N = sizeof(array) / sizeof(array[0]);
   // Given string
   string hex = "1BCFE234";
   changeArr(array, N, hex);
   return 0;
}

輸出

The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is 
6 0 8 112 13 0 

時間複雜度 – O(N*S),其中 N 是陣列長度,S 是字串長度。

空間複雜度 – O(S),因為我們需要儲存修改後的字串。

更新於: 2023年8月18日

63 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告