117. Parse the Integer
mediumstringparsing
Convert s to a 32-bit signed integer:
- Skip leading spaces.
- Read an optional
+or-. - Read digits until the first non-digit (or the end).
- If there are no digits, the result is
0. - Clamp to
[-2³¹, 2³¹ − 1].
Example 1
Inputs = "42"
Output42
Example 2
Inputs = " -042"
Output-42
Example 3
Inputs = "1337c0d3"
Output1337
Example 4
Inputs = "words 987"
Output0
Constraints
- 0 ≤ s.length ≤ 200