Easy and fast Java solution with 3ms runtime


  • 0
    C
    1. convert String to char array

    2. judge the first character

    3. convert one after another

    4. judge if overflow

       public class Solution {
           public int myAtoi(String str) {
               str = str.trim();
               char[] c = str.toCharArray();
               if (c.length == 0) {
                  return 0;
               }
           
               int i = 0;
               boolean isMinus = false;
               if (c[0] == '-') {
                   isMinus = true;
                   i++;
               } else if (c[0] == '+') {
                   i++;
               } else if (!(c[0] >= '0' && c[0] <= '9')) {
                   return 0;
               }
      
               long result = 0;
               while (i < c.length && c[i] >= '0' && c[i] <= '9') {
                   result = result*10 + c[i++] - '0';
                   if (result >= Integer.MAX_VALUE) {
                       if (isMinus) {
                           if (result > Integer.MAX_VALUE) {
                               return Integer.MIN_VALUE;
                           }
                       } else {
                           return Integer.MAX_VALUE;
                       }
                   }
               }
               return isMinus ? (int)(0-result) : (int)result;
           }
       }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.