首页 > 其他 > 详细

【数学】stoi

时间:2015-03-26 10:47:22      阅读:176      评论:0      收藏:0      [点我收藏+]

题目:leetcode

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 do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are 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 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, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because 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 returned.

注意:

int i = -1;
string s = "010";
if (-1<s.size())//判断条件为假,因为编译器把-1转化为一个很大的正数!



 int atoi(string str) {
        if(str.empty())//判断是否空
        return 0;
        int i,size=str.size();
        for(i=-1;i<size-1 && str[i+1]==' ';i++);
        if(i==str.size()-1)//消除空格后是否空
        return 0;
        string number(str.begin()+i+1,str.end());
        
        bool isnegetive=false;
        if(number[0]=='-')
        {
            isnegetive=true;
            number.erase(number.begin());
        }
        else if(number[0]=='+')//这里要用else if
            number.erase(number.begin());
        
        int j;
        for(j=0;j<number.size();j++)
        {
            if(number[j]<'0' || number[j]>'9')
            break;
        }
        if(j==0)//没有数字
        return 0;
        if(j!=number.size())
        {
            number.erase(number.begin()+j,number.end());//消除结尾非数字字符
        }
       
        if(number.size()>10)//若长度大于十,肯定超过int类型的表示范围
        {
             if(isnegetive)
       {
           int res=0x80000000;
           return res;
       }
      else
       {
           int res=0x7fffffff;
           return res;
       }
        }
        
        long long int result=0;
        for(int i=0;i<number.size();i++)
        {
            result=result*10+number[i]-'0';
        }
        long long int fushumax=0x80000000;
        if(isnegetive && result>fushumax)//负数溢出
       {
           int res=0x80000000;
           return res;
       }
       if(!isnegetive && result>fushumax-1)//正数溢出
       {
           int res=0x7fffffff;
           return res;
       }
        
        int res=result;
        if(isnegetive)
        res*=-1;
        
        return res;
       
    }


【数学】stoi

原文:http://blog.csdn.net/bupt8846/article/details/44646729

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!