String to Integer(atoi)
https://leetcode.com/problems/string-to-integer-atoi/
最后更新于
这有帮助吗?
https://leetcode.com/problems/string-to-integer-atoi/
最后更新于
这有帮助吗?
Implement the myAtoi(string s)
function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi
function).
The algorithm for myAtoi(string s)
is as follows:
Read in and ignore any leading whitespace.
Check if the next character (if not already at the end of the string) is '-'
or '+'
. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored.
Convert these digits into an integer (i.e. "123" -> 123
, "0032" -> 32
). If no digits were read, then the integer is 0
. Change the sign as necessary (from step 2).
If the integer is out of the 32-bit signed integer range [-231, 231 - 1]
, then clamp the integer so that it remains in the range. Specifically, integers less than -231
should be clamped to -231
, and integers greater than 231 - 1
should be clamped to 231 - 1
.
Return the integer as the final result.
Note:
Only the space character ' '
is considered a whitespace character.
Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
0 <= s.length <= 200
s
consists of English letters (lower-case and upper-case), digits (0-9
), ' '
, '+'
, '-'
, and '.'
.
Even if this function has already be implemented in STL, it is still valuable to know how to write it by yourself. As there is a comment in the discussion part, Ironically, the most disliked problem on LeetCode is the type of problem that you would encounter the most as a real world software developer, we should be diligent to edge cases in this problem. The examples and constraints in this problem are very helpful.
The key of this problem is about edge cases, e.g. overflow and underflow:
Since integer must be within the 32-bit signed integer range.
If the integer is positive, for 32 bit signed integer, INT_MAX is 2147483647 (2^31-1). To avoid integer overflow, we must ensure that it doesn't exceed this value. This condition needs to be handled when the result is greater than or equal to INT_MAX/10 (214748364)
Case 1). If result = INT\_MAX/10, it would result in integer overflow if next integer character is greater than 7. (7 in this case is last digit of INT_MAX(2147483647). We can use INT_MAX%10 to generically represent the last digit.
Case 2). If result>INT_MAX/10, we are sure that adding next number would result in integer overflow.
This holds for negative integer as well. In the 32-bit integer, INT_MIN value is -2147483648 (-2^31). As the last digit is greater than 7 (INT\_MAX% 10), integer underflow can also be handled using the above approach.
We must return INT_MAX in case of integer overflow and INT_MIN in case of integer underflow.
Also, it must be noted that in some languages like Python, an integer value is not restricted by the number of bits. Handling of overflow and underflow won't be required in such cases. We could simply check if the value of an integer is out of specified range [-2^31, 2^31-1]
Discard all the whitespaces at the beginning of the string.
There could be an optional sign of a numerical value +/-+/−. It should be noted that the integer is positive by default if there is no sign present and there could be at most one sign character.
Build the result using the above algorithm until there exists a non-whitespace character that is a number (00 to 99). Simultaneously, check for overflow/underflow conditions at each step.
When optional sign is more than 1, the result must be 0.