knakul853

Untitled

Jun 19th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6. public:
  7.     int myAtoi(string str) {
  8.        
  9.         int n = (int)str.size();
  10.         int i=0;
  11.        
  12.         while( i<n && str[i] == ' ')i++;
  13.          
  14.         if( i == n) return 0;
  15.         bool is_neg = false;
  16.         if(str[i] == '-'){is_neg = true;i++;}
  17.        
  18.         else if(str[i] == '+') i++;
  19.        
  20.         long long x =0;
  21.        
  22.         while( i < n && isdigit(str[i])){
  23.             x = x*10+(str[i]-'0');
  24.            
  25.             if( is_neg && -x < INT_MIN ) return INT_MIN;
  26.             if( !is_neg && x > INT_MAX ) return INT_MAX;
  27.             i++;
  28.         }
  29.        
  30.         if(is_neg)x=-x;
  31.         return x;
  32.  
  33.     }
  34. };
Add Comment
Please, Sign In to add comment