diff --git a/atoi.cpp b/atoi.cpp new file mode 100644 index 0000000..b88c6c4 --- /dev/null +++ b/atoi.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +int myAtoi(string str) { + int n=str.size(); + int i=0; + while(i='0' && str[i]<='9')){ + return 0; + } + long long ans=0; + for(i;i='0' && str[i]<='9'){ + if(sign==1){ + if(INT_MAX/10'7'){ + return INT_MAX; + } + } + if(sign==-1){ + if(INT_MAX/10'8'){ + return INT_MIN; + } + } + ans*=10; + ans+=str[i]-'0'; + } + else{ + break; + } + } + return sign*ans; + } \ No newline at end of file