Parse an Integer
Convert a string to a 32-bit signed integer:
- Skip leading spaces.
- Read an optional
+or-sign. - Read digits until a non-digit or the end.
- Clamp the result to .
If no digits are read, return 0.
Examples
EXAMPLE WALKTHROUGHplain
A guided view of the published example, not a trace of your code.
INPUT
["42"]TASKparse_integer()
EXPECTED OUTPUT
42Start with the input values shown in this example.
Example 1 of 3
Input: s = "42"
Output: 42
Explanation: plain
Input: s = " -42"
Output: -42
Explanation: spaces and sign
Input: s = "4193 with words"
Output: 4193
Explanation: stops at text
Hint 1
Walk an index through the string step by step.
Hint 2
Clamp at the end (Python integers don't overflow), but think about how you'd check overflow in C++ or Java.