Implement atoi
which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first nonwhitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of nonwhitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ‘ ‘ is considered a whitespace character.
Assume we are dealing with an environment that could only store integers within the 32bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, 231 − 1 or −231 is returned.
Example 1:

Explanation
This challenge is not easy, there are many corner cases in test cases. We need to handle two categories of tests:
 Overflow or underflow, we need to return INT_MAX or INT_MIN depends on it is signed or not
 Signed or not, and whether it is a valid number
Solution #1: Brute force
My first version of CPP is complicated, I use long long
to handle overflow issue:

Solution #2: An more simple implementation

Solution #3: Finitestate machine
This challenge involves complex string processing for parsing. It’s errorprone if we write it by hand.
Therefore, in order to analyze the processing of each input character in an organized way, we can use the concept of finitestate machine.
Our program has a state s
at each moment, and each time a character c
is entered from the sequence, it is transferred to the next state s'
according to the character c
. In this way, we only need to build a table covering all cases of mapping from s
and c
to s'
.
We can also represent the finitestate machine as a table:
state\char  space  +/  digits  other 

start  start  signed  number  wrong 
signed  wrong  wrong  number  wrong 
number  wrong  wrong  number  wrong 
wrong  wrong  wrong  wrong  wrong 
With this table we can easily implement the whole program, the initial state is start
and we change the state according to current character:

Join my Email List for more insights, It's Free!😋