This problem in Leetcode OJ is easy, but the pass rate is relatively low, the reason is to consider the situation is relatively low, very few people over it.
Topic
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.
Spoilers alert ... click to show requirements for atoi.
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.
Resolution
I guess not many people do not look at the following requirements to pass the bar!
This problem requires atoi and C + + implementation is not the same, for example, I think that does not meet the requirements of the return-1, and this problem requires the return of 0.
Therefore, it is necessary to explain the requirements of the topic:
1. First you need to discard the spaces in front of the string;
2. Then there may be a sign (note that only one, if there is more than one sign, then said that the string is not convertible, return 0. For example, there is a "+-2" in the test case;
3. The string can contain characters other than 0~9, and if a non-numeric character is encountered, only the part preceding that character, such as " -00123a66", is returned as "123";
4. If the range of int is exceeded, the boundary value (2147483647 or-2147483648) is returned.
In summary, the request is still a bit strange, do not look at the requirements is difficult to write, see also not necessarily understand the right.
"Java Code"
public class Solution {public int atoi (String str) {//1. Null or empty string if (str = = NULL || Str.length () = = 0) return 0; 2. Whitespaces str = Str.trim (); 3. +/-Sign Boolean positive = true; int i = 0; if (Str.charat (0) = = ' + ') {i++; } else if (Str.charat (0) = = '-') {positive = false; i++; }//4. Calculate real value Double tmp = 0; for (; i < str.length (); i++) {int digit = Str.charat (i)-' 0 '; if (Digit < 0 | | digit > 9) break; 5. Handle min & Max if (positive) {TMP = 10*tmp + digit; if (tmp > Integer.max_value) return integer.max_value; } else {tmp = 10*tmp-digit; if (TMP < integer.min_value) return integer.min_value; } } int ret = (int) tmp; return ret; }}
Refer to http://www.programcreek.com/2012/12/leetcode-string-to-integer-atoi/, code as follows:
public int atoi (String str) {if (str = = NULL | | str.length () < 1) return 0,//trim White spacesstr = Str.trim (); Char F lag = ' + '; Check negative or Positiveint i = 0;if (Str.charat (0) = = '-') {flag = '-'; i++;} else if (Str.charat (0) = = ' + ') {i++;} Use double to store resultdouble result = 0; Calculate Valuewhile (Str.length () > I && str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 ') {Resul T = result * + (Str.charat (i)-' 0 '); i++;} if (flag = = '-') result =-result; Handle Max and minif (Result > Integer.max_value) return integer.max_value; if (Result < Integer.min_value) return integer.min_value; return (int) result;
The code looks more concise, but the first one can jump out of the loop in time, and then decide whether to cross the line without counting.
"Leetcode" String to Integer (atoi) Problem solving report (Java)