Concise 6ms C++ solution


  • 0
    S
    int myAtoi(string str) {
    
      const char *c = str.c_str();
      for ( ; isspace(*c); c++);
    
      int sign = 1;
      if (*c == '-') {
        sign = -1;
      } else if (*c != '+') {
        c--;
      }
            
      int result = 0;
      while (isdigit(*++c)) {
        int tresult = result * 10 + (sign * (*c - '0'));
        if (tresult / 10 != result) {
          result = sign > 0 ? INT_MAX : INT_MIN;
          break;
        }
        result = tresult;
      }
            
      return result;
    }
    

Log in to reply
 

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