1. Title
String to Integer (atoi) (string-to-number conversion)
2. Address of the topic
https://leetcode.com/problems/string-to-integer-atoi/
3. Topic content
English: Implement atoi to convert a string to an integer.
English: Implement the Atoi function to convert the input string (string type) to Integer data (integer type)
Tip: The Atoi function you implement needs to meet the following characteristics
ignores all spaces (whitespace) before the first non-whitespace character of the string, the first non-whitespace character can be a sign, followed by a pure number, and the Atoi function converts these pure numbers to integers and returns
Any other character can be followed by a numeric character, but these non-numeric characters are ignored
If the first non-whitespace character in a string is not a number, the string is empty or consists only of spaces, it is not converted
Returns 0 if the string cannot be converted. Returns the maximum value of the integer number Int_max (2147483647) or the minimum value int_min (-2147483648) If the value is too large or too small
4. Method of solving Problems 1
You can use the Integer.parseint function in Java to solve the problem, but the Integer.parseint function and the atoi described in the topic are more restrictive on the input being received, so you need to convert the input string first to the form that Integer.parseint can convert (a pure number And the preceding sign is extracted first, and then called Integer.parseint). In addition, in the process of numerical conversion, it is necessary to judge the exception information, if it is due to overflow caused by the numberformatexception, the integer number is returned to the maximum or minimum value, otherwise, can not be converted processing, directly return 0.
The Java code is as follows:
/** * function Description:leetcode 8 - string to integer (atoi) * Developer:tsybius2014 * Development time: July 31, 2015 */public class Solution { /** * atoi function * @param str * @return */ public int myatoi (STRING&NBSP;STR) { //input is empty direct return 0 if (Str.isempty ()) { return 0; } str = str.trim (); &nbSp; char[] array = str.tochararray (); //judging the positive and negative of numbers boolean sign = true; if ( array[0] == '-') { sign = false; } //the first occurrence of the letter, ignore the following characters int index = -1; int signcount = 0; for (int i = 0; i < array.length; i++) { if (array[i] >= ' 0 ' && array[i] <= ' 9 ') { continue; } else if (array[i] == '-' | | array[i] == ' + ') { signcount++; //sign the second occurrence, ignore the following characters if (signcount > 1) { index = i; break; } continue; } else { index = i; break; } } //Intercept String if (index != -1) { Str = str.substring (0, index); //after interception is empty string or invalid string, return 0&NBsp; if (Str.isEmpty () | | str.equals ("-") | | str.equals ("+")) { return 0; } } //Value Conversion int result; try { result = integer.parseint (str); return (int) result; } catch (NUMBERFORMATEXCEPTION&NBSP;EX) //value overflow condition { if (sign ) { return integer.max_value; } else { return Integer.MIN_VALUE; } } catch (Exception &NBSP;EX) //other anomalies { return 0; } }}
5. method of solving problems 2
Method 2 is another form of implementation of Method 1, because Method 1 uses a comparison of the original method to parse the string, so I think I can use regular expressions directly match this number, to the Integer.parseint function.
The Java code is as follows:
import java.util.regex.pattern;import java.util.regex.matcher;/** * function Description: LeetCode 8 - String to Integer (atoi) * developer:tsybius2014 * Development Date: July 31, 2015 */public class Solution { /** * atoi functions * @param str * @return */ public int myatoi (String &NBSP;STR) { str = str.trim (); //regular Match pattern p = pattern.compile ("^ ([+-]|) [0-9]+ "); matcher m = P.matcher (str); if (M.find ()) { str = str.substring (M.start (), m.end ()); } else { return 0; } //judgment number of the sign boolean sign = true; if (str.charAt (0) == '-') { sign = false; } //Value Conversion int result; try { result = integer.parseint (str); return (int) result; } catch (Numberformatexception ex) // Case of a value overflow { if (sign) { return Integer.MAX_VALUE; } else { return Integer.MIN_VALUE; } } catch ( EXCEPTION&NBSP;EX) //other anomalies { return 0; } }}
6. method of solving problems 3
If the above two methods are relatively lazy approach, then the third method can be called "normal solution", pure manual parsing string. It is important to note that, during the calculation, the result needs to be temporarily in a long type of data, which can be used to determine if there is an integer overflow in the same situation and return the correct return value (Integer.max_value or Integer.min_ VALUE).
The Java code is as follows:
/** * function Description:leetcode 8 - string to integer (atoi) * Developer:tsybius2014 * Development time: July 31, 2015 */public class Solution { /** * self-made atoi function * @param str string to convert * @return converted numbers */ public int myatoi (STRING&NBSP;STR) { str = str.trim (); int sign = 0; //symbol: 0 unsigned, 1 positive, 2 negative long result = 0; //Calculation Results for (Char ch : str.tochararray ()) { if (ch == ' + ') { //first encounter symbol record, second encounter symbol ignore if (sign != 0) { break; } sign = 1; } else if (ch == '- ') { if (sign != 0) { break; } sign = -1; } else if (ch >= ' 0 ' && ch <= ' 9 ') { //encounters a number and checks if the maximum value of the integer is exceeded result *= 10; result += (ch - ' 0 '); if (Result > integer.max_value) { if (sign >= 0) { return Integer.MAX_VALUE; } else { return Integer.MIN_VALUE; } } } else { //encountered other characters directly exiting the loop break; } } if (sign >= 0) { return (int) result; } else { return (int)-result; } }}
END
Leetcode:string to Integer (atoi)