String to Integer (atoi)

Source: Internet
Author: User

https://oj.leetcode.com/problems/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-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.

Spoilers alert ... click to show requirements for atoi.

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.

Problem Solving Ideas:

Consider a variety of situations, more tired. It is worth noting that the method of judging Integer.max_value and Min_value, the previous log said. The penultimate is already greater than MAX/10, certainly greater than, equal to the words, judging the last one.

 Public classSolution { Public intatoi (String str) {str=Str.trim (); intresult = 0; intminus = 1;  for(inti = 0; I < str.length (); i++){            if(i = = 0 && (str.charat (0) = = '-')) ) {minus=-1; Continue; }            if(i = = 0 && (str.charat (0) = = ' + ')) ) {minus= 1; Continue; }            if(i = = 0 && (str.charat (0) = = ' + ' | | Str.charat (0) = = '-') && str.length () > 1 &&! (Str.charat (1) >= ' 0 ' && str.charat (1) <= ' 9 ')){                return0; }            if((Str.charat (0) = = ' + ' | | Str.charat (0) = = '-') && str.length () > 1 &&! (Str.charat (1) >= ' 0 ' && str.charat (1) <= ' 9 ')){                return0; }            if((Str.charat (0) = = ' + ' | | (Str.charat (0) >= ' 0 ' && str.charat (0) <= ' 9 ')) && result > Integer.max_value/10 && str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 '){                returnInteger.max_value; }            if((Str.charat (0) = = ' + ' | | (Str.charat (0) >= ' 0 ' && str.charat (0) <= ' 9 ')) && result = = INTEGER.MAX_VALUE/10 && str.charat (i) > ' 7 ' && str.charat (i) <= ' 9 '){                returnInteger.max_value; }            if(Str.charat (0) = = '-' && result > INTEGER.MAX_VALUE/10 && str.charat (i) >= ' 0 ' && Str.char at (i) <= ' 9 '){                returnInteger.min_value; }            if(Str.charat (0) = = '-' && result = = INTEGER.MAX_VALUE/10 && str.charat (i) > ' 8 ' && Str.charat ( i) <= ' 9 '){                returnInteger.min_value; }            if(i >= 0 &&!) (Str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 ')){                returnResult *minus; } result= result * + Str.charat (i)-' 0 '; }        returnResult *minus; }}

If the chaos here, in fact, you can use a finite state machine method, but also dead, to STR initialization state=0. Encounter the first one is ' + ' or '-', state=1, number, state=2, etc. If State==1,if (state==2), then the individual state is processed in the branch, and the above processing is actually the same. Someone put the code in the discuss as follows.

Https://oj.leetcode.com/discuss/22247/my-o-n-c-solution-with-finite-state-machine

String to Integer (atoi)

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.