8. String to Integer (atoi)

MediumString

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm reads in and ignores any leading whitespace, then reads an optional '+' or '-' sign, then reads in the digits until a non-digit character or the end. If no digits were read, the result is 0. The value is clamped to the 32-bit signed integer range [-2^31, 2^31 - 1].

Examples

Input: -42

Output: -42

Explanation: Skip spaces, read sign '-', read digits 42.

Constraints

  • 0 <= s.length <= 200
  • s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
Loading...

Run checks all cases above. Submit evaluates all test cases.