給定字串中由連續數字組成的數字之和


問題陳述

我們給定一個字串 str,其中包含數字和字母字元。我們需要找到給定字串中所有由連續數字序列表示的數字的總和。

示例

輸入

str = “12were43”

輸出

55

解釋

12 和 43 的和等於 55。

輸入

str = “1a2c3d”

輸出

6

解釋

1、2 和 3 的和是 6。

輸入

str = “werderfrewsf”

輸出

0

解釋

由於字串不包含任何數字,因此輸出為 0。

我們解決問題的邏輯是從給定的字串中提取所有數字並求和。

方法 1

在這種方法中,我們將使用 isDigit() 方法來檢查當前字元是否為數字。此外,如果當前字元是數字,我們將當前數字的值乘以 10 並將當前字元新增到數字中。

演算法

  • 步驟 1 − 將“number”和“sum”變數初始化為零。

  • 步驟 2 − 遍歷字串並使用 isDigit() 方法檢查當前字元是否在 0-9 之間。

  • 步驟 3 − 如果當前字元是數字,則將數字值乘以 10,並加上當前數字值。

  • 步驟 4 − 如果當前字元不是數字,則將“number”變數的值新增到“sum”變數中,並將“number”變數的值更新為零。

  • 步驟 5 − 迴圈迭代完成後,將“number”的值新增到“sum”變數中,並返回 sum 變數的值。

示例

#include <bits/stdc++.h>
using namespace std;
// function to return the sum of the consecutive number present in the string
int getSumOfDigits(string str){
   // store the current number
   int number = 0;
   // Stores total sum
   int sum = 0;
   // Traverse the string
   for (auto &ch : str){
      // If the current character is between '0' and '9', append it to the number
      if (isdigit(ch)) {
         number = number * 10 + ch - '0';
      } else {
         // 	if the current character is not between '0' and '9', add 'number' to the sum and reset 'number'
         sum += number;
         number = 0;
      }
   }
   // if the number is greater than 0, add it to sum
   sum += number;
   return sum;
}
int main(){
   string str = "6we24er5rd6";
   cout << "The sum of consecutive digits in the given string is - " << getSumOfDigits(str);
   return 0;
}

輸出

The sum of consecutive digits in the given string is - 41
  • 時間複雜度 − O(n),因為我們使用單個迴圈。

  • 空間複雜度 − O(1),因為我們沒有使用任何額外的空間。

方法 2

在這種方法中,我們使用字元的 ASCII 值來檢查當前字元是否為數字。此外,我們將字元追加到“number”變數中,直到我們在字串中獲得數字,並使用 atoi() 方法從字串中提取數字。

演算法

  • 步驟 1 − 定義“number”變數並將其初始化為空字串。此外,定義“sum”變數並將其初始化為 0。

  • 步驟 2 − 使用 for 迴圈遍歷字串並獲取字串的每個字元。

  • 步驟 3 − 如果 c-'0' 大於或等於零且小於或等於 9,則表示當前字元為數字。

  • 步驟 4 − 如果當前字元是數字,則將其追加到“number”字串。

  • 步驟 5 − 如果當前字元不是數字,則使用 c_str() 方法將數字字串轉換為字元陣列,並將其作為 atoi() 方法的引數傳遞以將字串轉換為數字。此外,使用“”值更新數字字串。

    如果字串可轉換為數字,則 atoi() 方法返回一個數字;否則,它返回零。

  • 步驟 6 − for 迴圈迭代完成後,再次使用 atoi() 方法將字串轉換為數字並將其新增到 sum 值中。

示例

#include <bits/stdc++.h>
using namespace std;
// function to return the sum of the consecutive numbers present in the string
int getSumOfDigits(string str){
   string number = "";
   // to store the sum of all the consecutive numbers
   int sum = 0;
   // traverse the string
   for (char c : str){
      // if the current character is between 0 to 9
      if (c - '0' >= 0 && c - '0' <= 9){
         // append it to the number string
         number += c;
      }
      // if the current character is an alphabet
      else {
         // convert string to an array of characters and pass it to atoi() function
         sum += atoi(number.c_str());
         // reset temporary string to empty
         number = "";
      }
   }
   // if the number is greater than 0, add it to sum
   sum += atoi(number.c_str());
   return sum;
}
int main(){
   string str = "11aa32bbb5";
   cout << "The sum of consecutive digits in the given string is - " << getSumOfDigits(str);
   return 0;
}

輸出

The sum of consecutive digits in the given string is - 48
  • 時間複雜度 − O(N)

  • 空間複雜度 − O(1)

方法 3

在這種方法中,我們使用正則表示式查詢數字的所有匹配項。之後,我們可以將字串轉換為數字並將其新增到 sum 變數中。

演算法

  • 步驟 1 − 定義正則表示式模式。

  • 步驟 2 − 使用 regex_search() 方法查詢數字字串的匹配項。

  • 步驟 3 − 使用 while 迴圈進行迭代,只要我們找到匹配項。

  • 步驟 4 − 在 while 迴圈中,使用 stoi() 方法將字串轉換為數字並將其新增到 sum 變數中。

  • 步驟 5 − 此外,使用 match().suffix() 方法更新字串。因此,我們不會獲得重複的匹配項。

示例

#include <bits/stdc++.h>
using namespace std;
// Function to calculate the sum of the numbers found in the string
int getSumOfDigits(string str){
   // regex pattern to find the numbers in the string
   regex pattern("d+");
   smatch match;
   // variable to store the sum of the numbers
   int sum = 0;
   // using the regex_search() function to find the numbers
   while (regex_search(str, match, pattern)){
      // adding the numbers to the sum variable
      sum += stoi(match[0].str());
      // update the string
      str = match.suffix().str();
   }
   return sum;
}
int main(){
   // input alphanumeric string
   string str = "abc23@12";
   cout << "The sum of consecutive digits in the given string is - " << getSumOfDigits(str);
   return 0;
}

輸出

The sum of consecutive digits in the given string is - 0
  • 時間複雜度 − O(N),因為正則表示式透過遍歷字串來查詢匹配項。

  • 空間複雜度 − O(1)

結論

我們學習了三種不同的方法來查詢字串中出現的連續數字的和。最後一種方法是程式碼最最佳化的,因為它使用了正則表示式。但是,對於初學者來說,使用正則表示式可能很困難。

更新於: 2023-07-18

289 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.