Appearance
Extracting Numbers
Problem Description
Given a string A. The string contains alphanumeric characters.
Find the sum of all numbers present in it.
Note: All the numbers will fit in a 32-bit signed integer.
Problem Constraints
1 <= |A| <= 10^5
Input Format
The first and only argument is a string A.
Output Format
Return an integer.
Example Input
Input 1:
A = "a12b34c"
Input 2:
A = "123"
Example Output
Output 1:
46
Output 2:
123
Example Explanation
Explanation 1:
The numbers are 12, 34.
12 + 34 = 46
Explanation 2:
The only number is 123.
Solution
swift
import Foundation
class Solution {
func solve(_ A: inout String) -> Int64 {
var total:Int64 = 0
var current:Int64 = 0
for c in A {
if c.isNumber{
current = current * 10 + (Int64(String(c)) ?? 0)
}else{
total += current
current = 0
}
}
total += current
return total
}
}