String to Integer (atoi)

Implement myAtoi(s) which converts a string to a 32-bit signed integer. The algorithm reads the string from left to right in four ordered phases: 1. Skip…

Implement `myAtoi(s)` which converts a string to a 32-bit signed integer. The algorithm reads the string from left to right in four ordered phases: 1. **Skip leading whitespace.** Ignore any leading `' '` characters. 2. **Read the sign.** If the next character is `'+'` or `'-'`, read it; this determines whether the result is positive or negative. If neither is present, the result is positive by default. 3. **Read digits safely.** Before accepting each consecutive digit, compare the current prefix and digit with the sign-specific 32-bit magnitude limit. If the guard passes, update `value = value * 10 + digit`. 4. **Stop or clamp.** Stop at the first non-digit or the end of the string. If a digit would cross the signed 32-bit range `[-2147483648, 2147483647]`, clamp without storing that oversized value. If no digits are read, return `0`. Indexing is **0-based** and leading spaces are meaningful (they must be skipped before the sign). `INT_MIN = -2147483648`, `INT_MAX = 2147483647`. **Example:** `" -42"` → skip 3 spaces, read sign `"-"`, read digits `"42"`, giving `-42`.

- `0 <= s.length <= 200` - `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'` (all ASCII). - The result is clamped to the signed 32-bit range `[-2147483648, 2147483647]`.

Example 1: input s = " -42"; output -42. Skip 3 leading spaces, read sign "-", then digits "42". Value -42 is within int32 range.

Example 2: input s = "4193 with words"; output 4193. No leading space or sign; read digits "4193", stop at the space.

Example 3: input s = "words and 987"; output 0. First non-space character is "w" — neither a sign nor a digit, so no digits are read; return 0.

Category: Strings. Topic: strings. Difficulty: Medium. Patterns: string-processing.