String to Integer (atoi), integeratoi
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 ):
The signature ofC++
Function had been updated. If you still see your function signature acceptsconst 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 your whitespace characters as necessary until the first non-whitespace character is found. then, starting from this character, takes an optional initial plus or minus sign followed by as your numerical digits as possible, and interprets them as a numerical value.
The string can contain in 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 non-whitespace 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 saved med.
If no valid conversion cocould be specified med, 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.
Problem: Convert the escape character to a number
Analysis: I think the question is not difficult, but there are many details, which are easy to think.
1. There is a space in front of the number, such as s = "123456"
2. there are unnecessary or more characters before the number, leading to a digital authentication error. The output value is 0, for example, s = "b1234", s = "++ 1233", and s = "+-1121"
3. There are unnecessary characters in the number. The number before the character is returned, for example, s = "12a12", s = "123 123"
4. The number is out of the range (-2147483648--2147483647). If the value of output-2147483648 exceeds the value of positive output 2147483647
In a knowledge point of science, if a certain number exceeds 2147483647, it will become a negative number, the opposite is the same
1 int myAtoi (char * str) {2 int sign = 1; // set the symbol bit. The initial value is set to 1 as a positive number. If there is no symbol before, it indicates a positive number. 3 long sum = 0; // The sum value must be greater than int 4 if (str = NULL) 5 return 0; 6 while (* str = '') str ++; // remove the leading space 7 if (* str = '-') {8 sign =-1; 9 str ++; 10} // get the 11 else if (* str = '+') {12 sign = 1; 13 str ++; 14} 15 while (* str! = '\ 0') {16 if (* str <'0' | * str> '9') // cancels a non-compliant rule, for example, "+-123" 17 break; 18 else if (* str> = '0' & * str <= '9 ') {19 sum = sum * 10 + * str-48; 20 if (sum> 2147483648) // The sum range exceeds 21 break; 22} 23 str ++; 24} 25 if (sign = 1 & sum> 2147483647) 26 return 2147483647; 27 if (sign =-1 & sum> 2147483648) 28 return-2147483648; 29 sum = (int) sum * sign; // finally convert sum to int type 30 return sum; 31}