Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. public class Solution {
  2. public int myAtoi(String str) {
  3. if ("".equals(str)) {return 0;}
  4. int i = 0, sign = 1, length = str.length();
  5. double base = 0;
  6. while (str.charAt(i) == ' ') {i++;}
  7. if (str.charAt(i) == '-' || str.charAt(i) == '+') {
  8. sign = str.charAt(i++) == '-' ? -1 : 1;
  9. }
  10. while (i < length && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
  11. if ((base = base * 10 + (str.charAt(i++) - '0')) > Integer.MAX_VALUE) {
  12. return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
  13. }
  14. }
  15. return sign * (int)base;
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement