String to Integer
Turn s into an integer like this:
- Skip any spaces at the start.
- Read an optional
+or-sign. - Read digits until the first character that is not a digit (or the end).
- If there were no digits, the result is 0.
- Clamp the result to the range from
-2147483648to2147483647.
Return the integer.
Example 1
s " -42abc"
returns -42
Spaces skipped, sign -, digits 42: result -42.
Example 2
s "words 987"
returns 0
Starts with a letter, so no digits are read: 0.
Constraints
0 <= len(s) <= 50shas letters, digits, spaces,+,-and.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.