This question. is to convert the string to an integer. Note is an integer, I see an integer when the sigh of relief, no decimal judgment should be better done. and the basic conversion function I think every programmer can not forget:
res=res*+ (str[i]-'0');
In fact, it is such a sentence of the matter, however, the passage rate of the problem is only 13%, in more than 200 titles ranked fifth. I do not want to see the hint to write some judgments, but still can not escape the end of WA.
Look at the following pile of requirement, or there is a great probability wa.
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.
This function can discard as many whitespace characters as possible when the first non-whitespace character appears. Then start with this non-whitespace character, get an initial plus or minus sign, and then the numeric character will be converted to a number.
This string can contain other characters after a number has been formed, and the function should not be affected by them and ignore them.
If the first non-whitespace character is not a valid numeric character, or if the sequence does not exist, it is empty or is all white space characters, and no conversions are made.
If no conversion is made, the function should return a value of 0. If the correct value is out of bounds, you should return 2147483647 or-2147483648.
Provide some error results collected in Dicuss and examples of WA:
Input |
Output |
Expected |
"B11228552307" |
2147483647 |
0 |
"+-2" |
2 |
0 |
" -0012a42" |
0 |
-12 |
"+0 123" |
123 |
0 |
"10522545459" |
1932610867 |
2147483647 |
I have to say that the test sample is too dangerous.
The following is the AC code:
1 classSolution {2 Public:3 intMyatoi (stringstr) {4 5 Longres=0;6 intflag=1;7 intI=0;8 intCount=0;9 while(str[i]==' ')Teni++; One if(str[i]=='-') A { -flag=-1; i++; -count++; the } - Else - if(str[i]=='+'){ -flag=1; i++; +count++; - } + Else A { at if(str[i]>'9'|| str[i]<'0') - return 0; - } - if(count>1)return 0; - intNumcount=0; - for(; I<str.length (); i++) in { - if(str[i]<='9'&&str[i]>='0'){ tonumcount++; +Res=res*Ten+ (str[i]-'0'); - if(numcount> One) the { *res=2147483649; $ Break;Panax Notoginseng } - Continue; the } + Else A Break; the } + if(res>2147483647&&flag==1)return 2147483647; - if(res>2147483648&&flag==-1)return-2147483648; $ intFinal=Res; $ returnfinal*Flag; - } -};
Flag is used to determine the positive or negative, count is used to determine the number of signs, if read non-numeric characters to the end, Numcount is used to determine the bounds. I used a long this data type first conversion, so that in a range of less than 11 numbers, long is not out of bounds, directly assign the final result to an int type.
After AC the official gives the solution to understand the lock:
To deal with overflow, inspect the current number before multiplication. If the current number is greater than 214748364, we know it was going to overflow. On the other hand, if the current number was equal to 214748364, we know it would overflow only when the current digit is greater than or equal to 8.
Average rating:3.7 (305 votes)
The main idea is to determine whether the current number is 214748364 before the next multiply, if the number is greater than the continuation of the multiplication is bound to cross, if at this time to add the number is 8 or above is also out of bounds (positive negative numbers are divided into 7 and 8 discussion).
This problem is simple, but it is still a lot to consider when writing, there are three ways to judge the way out of bounds:
The first is this kind of official giving practice, recommended to use;
The second is to use a larger data type to multiply this data and break directly when the number of bits exceeds 10;
The third is to use string matching, which is the method I used in reverse integer.
PS: On the 32-bit compiler:
The value range of unsigned int is: 0-4294967295;
The value range of int is 2147483648-2147483647 (2 of 32);
The value of long is the same on the 32-bit compiler and int, but there are 8 bytes on the 64-bit compiler, so it is much larger than the int representation. The use of a long in the subject can be through the example of the value overflow, indicating that the evaluation machine is also 64-bit.
The maximum value of long long: 9223372036854775807
The minimum value of long long:-9223372036854775808
Maximum value of __int64:9223372036854775807
Minimum value of __int64:9223372036854775808
String to Integer (atoi)-Complex test