← 返回 netflix 的题目列表String to Integer (atoi)
类型:qbank
Implement myAtoi: skip leading whitespace, read an optional sign, parse digits until a non-digit, and clamp to the 32-bit signed range.
String to Integer (atoi)
Implement myAtoi: skip leading whitespace, read an optional sign, parse digits until a non-digit, and clamp to the 32-bit signed range.
SWE
string
parsing
state-machine
medium
Frequency
Single report
Last asked
2026-03-03
Stage
phone-screen · onsite-coding
String to Integer (atoi)
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
The algorithm for myAtoi(string s) is as follows:
Ignore any leading whitespace (" ").
Check if the next character is '-' or '+'; read the sign if present.
Read digits until the next character is non-digit or the end of the string.
Convert the digits to an integer. If no digits were read, return 0.
Clamp the value to the 32-bit signed range [-2^31, 2^31 - 1].
Examples
Example 1:
Input: s = "42"
Output: 42
Example 2:
Input: s = " -042"
Output: -42
Explanation:
Skip leading spaces, read sign, then parse digits until a non-digit appears.
Example 3:
Input: s = "1337c0d3"
Output: 1337
Constraints
0 <= s.length <= 200
s consists of English letters, digits (0-9), space (' '), and symbols ('+', '-', '.').
The returned integer must be clamped to the 32-bit signed range.