daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int myAtoi(string a) {
  2. int n=a.length();
  3. int i=0;
  4. bool neg=0;
  5. while(i<n&&a[i]==' ')
  6. i++;
  7. if(i==n)
  8. return 0;
  9. if(a[i]=='+'||a[i]=='-'||a[i]>='0'&&a[i]<='9')
  10. {
  11.     if(a[i]=='-'||a[i]=='+')
  12.     {
  13.         if(a[i]=='-')
  14.         neg=1;
  15.         i++;
  16.     }
  17.     int ans=0;
  18.     while(i<n&&a[i]>='0'&&a[i]<='9')
  19.     {
  20.         int prev=ans;
  21.         ans=(ans*10)+a[i]-'0';
  22.         if(ans%10!=(a[i]-'0'))
  23.         {
  24.             if(neg)
  25.             return INT_MIN;
  26.             else
  27.             return INT_MAX;
  28.         }
  29.         i++;
  30.     }
  31.     if(neg)
  32.     return -ans;
  33.     else
  34.     return ans;
  35. }
  36. else
  37. {
  38.     return 0;
  39. }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top