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 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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click on the Reload button to reset your Code definition.
The simple point of this article is to turn the numbers in the string into plastic, but there are a few requirements for the topic: 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 was not a valid integral number, or if no such sequence exists because either str was 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 translation probably means:
1, first discard the white space character before the string to know to sweep to the first non-whitespace character, and then the string of a valid numeric value and one of its preceding sign together with an int;
2, in or after the legal numeric substring, may contain other non-numeric special characters, ignoring these substrings;
3, if the first non-empty string is not the integer value of the Dharma, or there is no, even the string may be a special character or a null character, then do not convert 0;
4, if not effectively converted, then return 0, if there is a return value beyond the boundary, then return the boundary value;
first, the idea:Although the subject belongs to a relatively simple level, but without consideration of all possible circumstances, there will always be the omission of some details of the results, resulting in the submission of a new unexpected situation always occurs. such as: "+-15651", "* 6644" the first occurrence of the string can not constitute a number of the case is illegal, directly return 0. The idea is as follows: First scan to the first non-null character, and then consider two cases, the first is the first non-null character is ' + ' or '-', if you continue to determine whether the next character is a number, is not illegal, the other is the first non-null character is a number, that can continue to scan the digital conversion. In addition to the above two cases, the others are illegal.
Second, Java program
public class Solution {public int myatoi (String str) {int SL = str.length (); int index=0; Char TEMPC; Gets the character Boolean flag = false; Negative sign int firsti=-1; Long re=0; if (sl<=0) return 0; 1. Start to the first non-empty character while (Str.charat (index) = = ') {index++; if (INDEX>=SL)//exceeds the string length of return 0; }//2. Sweep to the first non-null character, judging whether it is legal TEMPC = Str.charat (index); 2.1 If +-or value is, otherwise 0 if (tempc== '-' | | | tempc== ' + ' | | (tempc<= ' 9 ' &&tempc>= ' 0 ')) {//2.2 If the value jumps out to determine if ((tempc<= ' 9 ' &&tempc>= ' 0 ')) { }else//2.3 if +-+, then determine if the next number is the value {if (tempc== '-') flag = True //2.3.1 If-number, then set the flag bit index++; 2.4 If the next number is a value, then it is legal, not 0 TEMPC = str.chArat (index); if ((tempc<= ' 9 ' &&tempc>= ' 0 ')) {}else {return 0; }}}else//2.1 if +-or value, otherwise jump out of {return 0; }//3. Starts scanning for a valid number while (INDEX<SL) {TEMPC = Str.charat (index); if (tempc<= ' 9 ' &&tempc>= ' 0 ') {re = re*10 + (Str.charat (index)-' 0 '); if (re>=integer.max_value| | Re<=integer.min_value) break; }else {break; } index++; } if (flag==true) {re=-re; Re = Re<integer.min_value? Integer.MIN_VALUE:re; }else {re = re>integer.max_value? Integer.MAX_VALUE:re; } return (int) re; }}
Leetcode "8". String to Integer (atoi)--java implementation