Question
Formatted question description: https://leetcode.ca/all/8.html
8 String to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint:
Carefully consider all possible input cases.
If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (20150210):
The signature of the C++ function had been updated.
If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
[spoilers alert]... click to show requirements for atoi.
Requirements for atoi:
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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (2147483648) is returned.
Example 1:
Input: "42"
Output: 42
Example 2:
Input: " 42"
Output: 42
Explanation: The first nonwhitespace character is '', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The first nonwhitespace character is 'w', which is not a numerical
digit or a +/ sign. Therefore no valid conversion could be performed.
Example 5:
Input: "91283472332"
Output: 2147483648
Explanation: The number "91283472332" is out of the range of a 32bit signed integer.
Thefore INT_MIN (−231) is returned.
@tagstring
Algorithm
This question only needs to consider the numbers and symbols:

If the string starts with a space, all spaces are skipped to the first nonspace character, if there is no space, 0 is returned.

If the first nonspace character is the sign +/, then the sign is true or false. This question has a limitation, that is, in C++, both +1 and +1 are accepted, both It is 1, but in this question, it will return 0.

If the next character is not a number, it returns 0, regardless of the decimal point and natural numbers, but this is fine, at least it saves a lot of trouble.

If the next character is a number, it will be converted to plastic and saved. If a nonnumber appears next, the current result will be returned.

Also need to consider the boundary problem, if it exceeds the range of the integer number, use the boundary value to replace the current value.
Code
Java

public class String_to_Integer { // I just added "index < str.length()" to every step, to pass test case : " " class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. Empty string if(str.length() == 0) { return 0; } //2. Remove Spaces while(index < str.length() && str.charAt(index) == ' ') { index++; } //3. Handle signs if(index < str.length() && (str.charAt(index) == '+'  str.charAt(index) == '')){ sign = str.charAt(index) == '+' ? 1 : 1; index ++; } //4. Convert number and avoid overflow while(index < str.length()){ int digit = str.charAt(index)  '0'; if(digit < 0  digit > 9) { break; } //check if total will be overflow after 10 times and add digit if(Integer.MAX_VALUE/10 < total  Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } total = 10 * total + digit; index ++; } return total * sign; } } }

// OJ: https://leetcode.com/problems/stringtointegeratoi/ // Time: O(N) // Space: O(1) class Solution { public: int myAtoi(string s) { int i = 0, N = s.size(), sign = 1, ans = 0; while (i < N && s[i] == ' ') ++i; if (i < N && (s[i] == '+'  s[i] == '')) sign = s[i++] == '+' ? 1 : 1; while (i < N && isdigit(s[i])) { int n = s[i++]  '0'; if (ans > INT_MAX / 10  (ans == INT_MAX / 10 && n > INT_MAX % 10)) return sign == 1 ? INT_MAX : INT_MIN; ans = ans * 10 + n; } return ans * sign; } };

class Solution(object): def myAtoi(self, s): """ :type str: str :rtype: int """ s = s.strip() sign = 1 if not s: return 0 if s[0] in ["+", ""]: if s[0] == "": sign = 1 s = s[1:] ans = 0 for c in s: if c.isdigit(): ans = ans * 10 + int(c) else: break ans *= sign if ans > 2147483647: return 2147483647 if ans < 2147483648: return 2147483648 return ans