Leetcode008. String to Integer (atoi)

Source: Internet
Author: User

1 /*improvement on dealing with overflow accroding to this:2  * https://discuss.leetcode.com/topic/57452/c-solution-beats-1003  * 4 * Being careful about the Int_max and int_min5 * This atoi isn't thinking about special char like dot and so on.6  */7 8 classSolution {9  Public:Ten     intMyatoi (stringstr) { One        inti,nega_sign=1; A        Long Long intRe=0;//long long re to avoid overflow -         for(i=0; I<str.size (); i++)//Skip to all the blank in the front of the string -        { the            if(str[i]!=' ') Break; -        } -        if(str[i]=='+') i++;//deal with Signature -        Else if(str[i]=='-') {nega_sign=-1; i++;} +         while(I<str.size () &&isdigit (Str[i])) - /*When the string was over or the current char was not a valid integral number,no more conversion'll be PERFORMED. + */ A        { atre=re*Ten+str[i]-'0'; -                if(nega_sign==-1&&-1*re<=int_min)returnInt_min;//Overflow -                Else if(nega_sign==1&&re>=int_max)returnInt_max; -i++; -        } -        returnre*nega_sign; in     } -};

Leetcode008. 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.