knakul853

Untitled

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