String to Integer (atoi)

Medium

Description

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm: 1) Read and ignore leading whitespace. 2) Check for '-' or '+' sign. 3) Read digits until non-digit or end. 4) Clamp result to 32-bit signed integer range [-2^31, 2^31 - 1].

Examples

Input:s = "42"
Output:42
Explanation:

The parsed integer is 42.

Input:s = " -42"
Output:-42
Explanation:

Leading whitespace is ignored, and the sign is negative.

Input:s = "4193 with words"
Output:4193
Explanation:

Parsing stops at the space.

Constraints

  • 0 ≤ s.length ≤ 200
  • s consists of English letters, digits, ' ', '+', '-', and '.'.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!