215. String to Integer (atoi)

MediumStringString

Implement atoi: convert a string to a 32-bit signed integer. Skip leading whitespace, handle an optional +/- sign, read digits until a non-digit, and clamp to [-2^31, 2^31-1].

Input: A quoted string s.

Output: Integer — the parsed value.

Examples

Example 1
Input: "42"
Output: 42
Explanation: Simple integer.
Example 2
Input: "   -42"
Output: -42
Explanation: Leading spaces and sign.
Example 3
Input: "4193 with words"
Output: 4193
Explanation: Stops at non-digit.

Constraints

Asked by

BloombergAdobeMetaAmazonMicrosoftGoogle
Solve this problem in the editor →