Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int myAtoi(string str) {
- int n = (int)str.size();
- int i=0;
- while( i<n && str[i] == ' ')i++; // cleaning..
- if( i == n) return 0;
- bool is_neg = false;
- if(str[i] == '-'){is_neg = true;i++;}
- else if(str[i] == '+') i++;
- long long x =0;
- while( i < n && isdigit(str[i])){
- x = x*10+(str[i]-'0');
- if( is_neg && -x < INT_MIN ) return INT_MIN;
- if( !is_neg && x > INT_MAX ) return INT_MAX;
- i++;
- }
- if(is_neg)x=-x;
- return x;
- }
- };
Add Comment
Please, Sign In to add comment