Implement function atoi to convert a string to an integer.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
Example
"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1
Solution:
Detailed problem.
Three derails should be noted:
1. valid char c : c - '0',
2. not valid char: stop
3. '+' or '-' sign at the start, record it
4. Overflow: INT_MAX (2147483647) or INT_MIN (-2147483648) is returned
1 | public class Solution { |