diff --git a/711. String to Integer (atoi)/solution.h b/711. String to Integer (atoi)/solution.h new file mode 100644 index 0000000..a19ae6f --- /dev/null +++ b/711. String to Integer (atoi)/solution.h @@ -0,0 +1,65 @@ +class Solution { +public: + bool isOverflow(long long a, long long b){ + long long res=a*10+b-48; + if(res>INT_MAX) + return true; + return false; + } + + int myAtoi(string s) { + int c=0; + bool isminus=0; + bool nostart=0; + for(int i=0;i57){ + break; + } + else{ + nostart=1; + if(isOverflow(c,s[i])){ + if(isminus){ + c=INT_MIN; + isminus=0; + } + else{ + c=INT_MAX; + } + break; + } + + else{ + c=c*10+(s[i]-48); + } + + } + } + + if(isminus){ + c=c*-1; + } + + return c; + } +};