Guest User

Untitled

a guest
Jun 13th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment