This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41521063
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.
Notes: It is intended for the problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Requirements for Atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, the starting from this character, takes a optional initial plus or minus sign followed by as many numerical digits as P Ossible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which is ignored and has no Effe CT on the behavior of this function.
If the first sequence of non-whitespace characters in STR isn't a valid integral number, or if no such sequence exists be Cause 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 Returne D.
The knowledge you may learn through this article is:
(1) The maximum and minimum bounds of the basic data type int and the IsDigit () method of the character class are known.
(2) Learning to convert a string into a variety of situations that may occur in shaping.
(3) through the algorithm to improve their ability to analyze problems, and strive to take into account as many situations.
Note:
(1) int corresponding value range is -2147483648~ 2147483647
(2) The IsDigit () method of the character class is to determine whether the specified character is a number.
(3) The trim () method of the string class is to remove spaces before and after the string.
Ideas:
(1) Interpreting test instructions:
A: If the string is null returns 0, not empty, the string is traversed until it finds the first character that is a number, and returns 0 if no such character is found.
B: It is necessary to judge the case that the number is "+" or "-" before it appears.
C: If the resulting number is greater than the int maximum or less than the int minimum, then the maximum and minimum values of the corresponding int are returned respectively.
(2) Analysis:
A: For a given string, first remove the front and back space, to see if the remaining string size is empty, empty is returned 0;
B: If it is not empty, it is traversed until the first numeric character is present, and 0 is returned if the string does not appear with a numeric character;
C: If a numeric character occurs during traversal, we need to determine the character before the numeric character,
(a) If the first two characters of the numeric character are a combination of "+", "-", "", or an illegal character, return 0;
(b) If the first character of the numeric character is "+" or "-", then we judge it by positive and negative, and calculate the current value, for the calculated
value, we also need to cross-border judgment, and return if the return the corresponding cross-boundary value;
(c) If the first character of the numeric character is neither "+" nor "-", or is not a numeric character, it is an illegal character and returns 0;
(d) If this is not the case, then the numeric character is preceded by a numeric character, then processed according to the normal number, first
after judging whether the value is more and then returns the value after the judgment is given. Note that we begin to declare that sum is long, because
The value for sum may be larger than the maximum value of int, and once the most large value, sum will become negative, this should pay special attention to.
(3) Summary:
In fact, this algorithm problem is not difficult, but there are too many situations, it is difficult to consider all the situation, I also OJ several times to succeed. One of the things to note is:
the sum of the negative numbers is separated by a minus sign, saved and The variable must be defined by a long, and in the case of a negative sign, it is also required to be judged by a flag bit.
for example "-000124" this special characters of the sample. The following is my own thinking algorithm, the algorithm is a bit verbose, but think Road is more clear, I hope to help you.
The algorithm implementation code is as follows: (PS: I have limited technology, still can not give a short code, but OJ sure through, we have a better algorithm to share, thank you)
public static int atoi (String str) {if (str.length () = = 0) return 0;long sum = 0;STR = Str.trim (); int len = Str.length (); Bo Olean flag = false; When the first number appears, true if the number is followed, not the number is falseboolean negative = false; Whether the negative is judged for (int i = 0; i < len; i++) {char c = str.charat (i); Character.isdigit (c)) {if (flag = = True) {//When a number is encountered, a non-number is encountered again midway, the previous value is returned to return Isoutofrange (sum);} Continue;} if (Character.isdigit (c)) {//Determines whether the number is flag = true;//when the first number is encountered, if it is preceded by a combination of two characters "+" "-" ", then an illegal character, returned 0if (i-2 >= 0 & & (Str.charat (i-1) = = ' + ' | | Str.charat (i-1) = = '-' | | Str.charat (i-1) = = ') && (Str.charat (i-2) = = ' + '|| Str.charat (i-2) = = '-' | | Str.charat (i-2) = = ") {return 0;} else if (i-1 >= 0 && str.charat (i-1) = = ' + ') {//If there is only one" + "in front of it, then a positive sum = Sum * + integer.parseint (string.valueof (c));} else if (i-1 >= 0 && str.charat (i-1) = = '-') {//If there is only one "-" in front of it is negative negative = True;sum =-sum * 10-integer. parseint (String.valueof (c)); Negative numbers are added with a minus sign} ElSe if (i-1 >= 0&& (Str.charat (i-1)! = ' + ' | | str.charat (i-1)! = '-') &&! Character.isdigit (Str.charat (i-1))) {//If the preceding is not "+" or "-" is not yet a number, it returns 0return 0;} else {if (Sum < 0 | | negative = TRUE {//For 00123 need to add a flag bit to determine if (Sum < Integer.min_value) {//To determine whether to cross the return integer.min_value;} sum = sum * 10-integer.parseint (string.valueof (c));} else {if (sum > Integer.max_value) {//To determine whether to cross the return integer.max_value;} sum = sum * + integer.parseint (string.valueof (c));}}} return Isoutofrange (sum);} public static int isoutofrange (long sum) {if (sum > Integer.max_value) {return integer.max_value;} if (Sum < Integer.min_value) {return integer.min_value;} return (int) sum;}
String to integer--strings conversion algorithm considering various cases