classSolution { public: intmyAtoi(string s){ longlong res = 0; int k = 0; while (k < s.size() && s[k] == ' ') k ++ ; if (k == s.size()) return0; // 全是空字符
bool is_minus = false; if (s[k] == '+') k ++ ; elseif (s[k] == '-') k ++ , is_minus = true;
for (int i = k; i < s.size(); i ++ ) { if (isdigit(s[i])) { res = res * 10 + s[i] - '0'; if (res > INT_MAX + 1ll) res = INT_MAX + 1ll; if (res < INT_MIN) res = INT_MIN; } else { break; } }
if (is_minus) res = -res;
if (res > INT_MAX) res = INT_MAX; return res; } };