SHARE
TWEET

Untitled

a guest Apr 20th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
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