To solve this problem, we need to extract all the digits from a given string in the order they appear and form a number from these digits.
Approach
- Extract Digits: Iterate through each character in the input string and collect all the digits (0-9) in the order they appear.
- Form Number: Convert the collected digits into a single string and then into an integer. If there are no digits in the input string, return 0.
Solution Code
s = input().strip()
digits = ''.join([c for c in s if c.isdigit()])
print(int(digits) if digits else 0)
Explanation
- Extracting Digits: Using a list comprehension, we filter out non-digit characters from the input string. This ensures we only keep the digits in their original order.
- Forming the Number: The collected digits are joined into a single string. Converting this string to an integer automatically removes any leading zeros. If there are no digits, we return 0 as a default value.
This approach efficiently processes the input string in linear time, O(n), where n is the length of the input string, making it optimal for this problem. The solution handles all edge cases, including strings with no digits, and produces the correct result as per the given examples.


作者声明:本文包含人工智能生成内容。