"Leetcode-Interview algorithm classic-java Implementation" "008-string to Integer (atoi) (String to Integer)"

Source: Internet
Author: User

"008-string to Integer (Atoi) (string converted to integer)" "leetcode-Interview algorithm classic-java Implementation" "All Topics folder 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 the full input situation.

Thinking of solving problems

The leading character is + or-or not. The next step is to enter a number, and the number cannot be the maximum or the decimal that the integer can represent. Assume that the corresponding minimum or minimum value is returned.

Code Implementation
 Public  class solution {     Public intAtoi (StringStr) {if(Str==NULL||Str. Length () = =0) {//throw new NumberFormatException ("Invalid input string:" + str);            return 0; }//Suppose the string starts with a space        intStart =0;//Start looking for the first number that is not a space        BooleanPositive =true;//Whether it is positive to feel 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{//Other cases throw an exception//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) {//assumed to be positive                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; }    }}
Assessment Results

  Click on the picture, the mouse does not release. Drag a position to view the full picture in the new form after you release it.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46938417"

"Leetcode-Interview algorithm classic-java Implementation" "008-string to Integer (atoi) (String to Integer)"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.