leetcode8

leetcode8地址

int myAtoi(char* str) {
    long re = 0;
    bool flag = false;
    
    if(str[0]=='\0')
        return 0;
    
    for(int i=0;str[i]!='\0';i++)
        if(str[i]>='0' && str[i]<='9')
        {
            re = re*10+(str[i]-'0');
            if(re>2147483648)
                break;
        }    
        else if( ((i==0||str[i-1]==' ')&&str[i]==' ') || ((i==0||str[i-1]==' ') && (str[i]=='+'||str[i]=='-')))
        {
            flag = str[i]=='-'? true:false;
            continue;
        }
        else
            break;
    
    re = flag? re*-1:re;
    
    if(re>2147483647)
        re = 2147483647;
    else if(re<-2147483648)
        re = -2147483648;

    return re;
}
思路:思路很简单,问题主要在于需要关注所有特殊情况

你可能感兴趣的:(算法题)