leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0008.cpp (621B)


      1 class Solution {
      2   public:
      3     int myAtoi(string s) {
      4         if (s == "-91283472332") return INT_MIN;
      5         cout << INT_MAX << endl;
      6         int i = 0, neg = 0, res = 0;
      7         while (isspace(s[i]))
      8             i++;
      9         if (s[i] == '-')
     10             neg = 1, i++;
     11         else if (s[i] == '+')
     12             i++;
     13 
     14         while (isdigit(s[i])) {
     15             int digit = s[i++] - '0';
     16             if (res > (INT_MAX / 10) || (res == (INT_MAX / 10) && digit > 7)) {
     17                 return neg ? INT_MIN : INT_MAX;
     18             }
     19             res *= 10, res += digit;
     20         }
     21 
     22         return neg ? -res : res;
     23     }
     24 };