Leetcode--string to Integer (atoi)

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 for the problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.


Class Solution {Public:int atoi (const char *str) {string temp= "";        int loc=0;            while (Str[loc]! = ' + ') {if (str[loc]!= ') break;        loc++;        } Long long res=0;                    if (str[loc] = = '-') {while (Str[loc]! = ')} {if (Str[loc]! = ' 0 ')                Break            loc++;            } if (str[loc] = = ' + ') return 0; for (int i=loc+1; str[i]! = ' str[i '; i++) {if (str[i]<48 | |] >) BR                Eak                res = res*10+str[i]-48;            if (Res-1 > Int_max) return int_min;            } if (Res-1 > Int_max) return int_min;        Return-res;                    } else {while (Str[loc]! =) {if (Str[loc]! = ' 0 ')                Break         loc++;   } if (str[loc] = = ' + ') return 0;            if (str[loc] = = ' + ') loc = loc+1;                 for (int i=loc; str[i]!= ' + '; i++) {if (str[i]<48 | | str[i] >) break;                res = res*10+str[i]-48;            if (res > Int_max) return int_max;            } if (res > Int_max) return int_max;        return res; }    }};


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