My 8MS solution for c++


  • -1
    L

    class Solution {
    public:
    int myAtoi(string str)
    {
    long long result=0;
    int sign=1;
    int idx=0;
    int signCnt=0;
    int strLen = str.length();
    for(;(idx<strLen)&&(str[idx]==' ');idx++);
    if(idx==strLen)return 0;
    if(str[idx]=='-')
    {
    sign = -1;
    idx++;
    }
    else if(str[idx]=='+')
    {
    idx++;
    }
    for(;(idx<strLen)&&(str[idx]>='0')&&(str[idx]<='9');idx++)
    {
    result = result*10+((str[idx] -'0')*sign);
    if(result>=INT_MAX)return INT_MAX;
    else if(result<=INT_MIN)return INT_MIN;
    }
    return (int)result;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.