"008-string to Integer (Atoi) (string converted to integer)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.
Main Topic
Implement a Atoi function to convert a string into an orthopedic
Important: Consider all the input conditions.
Thinking of solving problems
The leading character is + or-or no, the next input is a number, and the number cannot be the maximum or the decimal that the integer can represent. Returns the corresponding minimum or minimum value if it is exceeded.
Code Implementation
Public class solution { Public intAtoi (StringStr) {if(Str==NULL||Str. Length () = =0) {//throw new NumberFormatException ("Invalid input string:" + str); return 0; }//If the string starts with a space intStart =0;//Start looking for the first number that is not a space BooleanPositive =true;//is a positive number default is True if(Str. charAt (start) = ="') { while(Str. charAt (start) = ="') {start++;if(Start >=Str. Length ()) {//input is full of spaces//throw new NumberFormatException ("Invalid input string:" + str); return 0; } } }if(Str. charAt (start) = ='-') {//First non-whitespace character-Positive =false; start++; }Else if(Str. charAt (start) = =' + ') {//The first non-whitespace character is +start++; }Else if(Str. CharAt (Start) >=' 0 '&&Str. CharAt (Start) <=' 9 ') {//The first non-whitespace character is a number returnCalStr, Start,true); }Else{//The exception is thrown in other cases//throw new NumberFormatException ("Invalid input string:" + str); return 0; }if(Start >=Str. Length ()) {//The first non-whitespace character is + or-but also the last character//throw new NumberFormatException ("Invalid input string:" + str); return 0; }if(Str. CharAt (Start) >' 9 '||Str. charAt (Start) <' 0 ') {//+ or-not the number followed//throw new NumberFormatException ("Invalid input string:" + str); return 0; }Else{returnCalStr, start, positive); } }Private intCal (StringStr,intStartBooleanPositive) {Longresult =0; while(Start <Str. Length () &&Str. CharAt (Start) >=' 0 '&&Str. CharAt (Start) <=' 9 ') {result = result *Ten+ (Str. CharAt (Start)-' 0 ');if(positive) {//If it is a positive number if(Result > Integer.max_value) {//throw new NumberFormatException ("Invalid input string:" + str); returnInteger.max_value; } }Else{if(-result < Integer.min_value) {//throw new NumberFormatException ("Invalid input string:" + str); returnInteger.min_value; }} start++; }if(positive) {return(int) result; }Else{return(int)-result; } }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46938417"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "008-string to Integer (atoi) (String to Integer)"