C++ solution


  • 0
    P
    
    class Solution {
    public:
        int myAtoi(string str) {
            if(str=="")
                return 0;
            
            unsigned int output=0;
            if(str[0]!='-')
                for(int i=0; i<str.size(); i++)
                {
                    output+=((str[i]-48)*pow(10,str.size()-1-i));
    
                }
            else
            {
                 for(int i=1; i<str.size(); i++)
                {
                    output+=((str[i]-48)*pow(10,str.size()-1-i));
    
                }  
                output=-output;
            }
            return output;
            
        }
    };```

Log in to reply
 

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