给定一个由数字[0 – 9]和小写字母组成的字符串S ,任务是计算字符串S 中存在的连续数字序列表示的所有数字的总和。
例子:
Input: S = “11aa32bbb5”
Output: 48
Explanation:
The consecutive sequence of numbers present in the string S are {11, 32, 5}.
Therefore, sum = 11 + 32 + 5 = 48
Input: s = “5an63ff2”
Output: 70
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如curr ,以存储当前的连续数字序列
- 遍历字符串的字符。
- 如果当前字符不是数字,则将curr的当前值添加到最终答案中。将curr重置为0 。
- 否则,将当前数字附加到curr 。
- 最后,返回最终答案。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to calculate the sum of
// numbers formed by consecutive
// sequences of digits present in the string
int sumOfDigits(string s)
{
// Stores consecutive digits
// present in the string
int curr = 0;
// Stores the sum
int ret = 0;
// Iterate over characters
// of the input string
for (auto& ch : s) {
// If current character is a digit
if (isdigit(ch)) {
// Append current digit to curr
curr = curr * 10 + ch - '0';
}
else {
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
}
ret += curr;
return ret;
}
// Driver Code
int main()
{
string S = "11aa32bbb5";
cout << sumOfDigits(S);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to calculate the sum of
// numbers formed by consecutive
// sequences of digits present in the string
static int sumOfDigits(String s)
{
// Stores consecutive digits
// present in the string
int curr = 0;
// Stores the sum
int ret = 0;
// Iterate over characters
// of the input string
for(char ch : s.toCharArray())
{
// If current character is a digit
if (ch >= 48 && ch <= 57)
{
// Append current digit to curr
curr = curr * 10 + ch - '0';
}
else
{
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
}
ret += curr;
return ret;
}
// Driver Code
public static void main(String[] args)
{
String S = "11aa32bbb5";
System.out.print(sumOfDigits(S));
}
}
// This code is contributed by splevel62.
Python3
# Python3 program for the above approach
# Function to calculate the sum of
# numbers formed by consecutive
# sequences of digits present in the string
def sumOfDigits(s) :
# Stores consecutive digits
# present in the string
curr = 0
# Stores the sum
ret = 0
# Iterate over characters
# of the input string
for ch in s :
# If current character is a digit
if (ord(ch) >= 48 and ord(ch) <= 57) :
# Append current digit to curr
curr = curr * 10 + ord(ch) - ord('0')
else :
# Add curr to sum
ret += curr
# Reset curr
curr = 0
ret += curr
return ret
# Driver Code
S = "11aa32bbb5"
print(sumOfDigits(S))
# This code is contributed by code_hunt.
C#
// C# Program to implement
// the above approach
using System;
class GFG
{
// Function to calculate the sum of
// numbers formed by consecutive
// sequences of digits present in the string
static int sumOfDigits(string s)
{
// Stores consecutive digits
// present in the string
int curr = 0;
// Stores the sum
int ret = 0;
// Iterate over characters
// of the input string
foreach(char ch in s)
{
// If current character is a digit
if (ch >= 48 && ch <= 57)
{
// Append current digit to curr
curr = curr * 10 + ch - '0';
}
else
{
// Add curr to sum
ret += curr;
// Reset curr
curr = 0;
}
}
ret += curr;
return ret;
}
// Driver code
static void Main() {
string S = "11aa32bbb5";
Console.WriteLine(sumOfDigits(S));
}
}
// This code is conributed by divyeshrabadiya07.
Javascript
输出:
48
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live