[Leetcode]41. String to Integer (atoi) strings to integers

Source: Internet
Author: User

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.

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.

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.

Solution: Do not see requirements really write can accepted. The following points are considered mainly:

(1) Enter an empty string: output 0;

(2) The input string is preceded by a number of spaces: remove the preceding space;

(3) The first non-whitespace character of the input string is a positive sign: The output corresponds to a change;

(4) The input string is not a valid character except the first character (' 0 ' to ' 9 '), and the output is 0;

(5) Invalid character after entering the first valid character of the string: direct interception of the preceding valid character conversion output; No need to consider the e/e of scientific notation, or the decimal point;

(6) The number represented by the input string exceeds the range represented by the integer type: Output Int_max or int_min.

classSolution { Public:    intMyatoi (stringstr) {        intn =str.size (); if(N <=0)return 0;//Enter an empty string                inti =0, res =0;  while(I < n && str[i] = =' ') ++i;//Remove the opening space                BOOLIsnegative =false; if(I < n && (str[i] = ='-'|| Str[i] = ='+'))//determines whether the first character is a positive or negative sign            if(str[i++] = ='-') Isnegative =true; if(I < n && (Str[i) <'0'|| Str[i] >'9'))return 0;//determines whether the first character is legal                 while(I < n && Str[i] >='0'&& Str[i] <='9')//Convert to Integer        {            if(Res > Int_max/Ten|| (res = = Int_max/Ten&& Str[i]-'0'>=8))//whether the maximum minimum value is exceeded                returnIsnegative?Int_min:int_max; Res= Res *Ten+ Str[i]-'0'; ++i; }                returnIsnegative? -Res:res; }};

[Leetcode]41. String to Integer (atoi) strings to integers

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.