String to Integer (atoi)

Medium

Convert s to a 32-bit signed integer:

  1. skip leading whitespace
  2. read an optional + or -
  3. read digits until a non-digit appears
  4. clamp the result to [-2^31, 2^31 - 1] and return 0 when no digits were read

Example 1

myAtoi()
Input
s = "42"
Output
42

Example 2

myAtoi()
Input
s = "   -42"
Output
-42
Constraints
0 <= s.length <= 200
`s` may contain letters, digits, spaces, `+`, `-` and `.`.

Topics

StringsSimulation

Implement myAtoi in the editor and return the answer — printing it is never graded.