Leetcode OJ: String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

实现atoi,其实没有这个提供的requirement,说实话还真很难A,特别是INT_MAX与INT_MIN的考虑。看代码吧。

 1 class Solution {
 2 public:
 3     int atoi(const char *str) {
 4         const char* p = str;
 5         while (*p == ' ')
 6             ++p;
 7         int flag = 1;
 8         int ret = 0;
 9         if (*p == '-') {
10             flag = -1;
11             p++;
12         }else if (*p == '+') {
13             flag = 1;
14             p++;
15         }
16         int preMax = INT_MAX / 10;
17         int preMod = INT_MAX % 10 + '0';
18         while (*p <= '9' && *p >= '0') {
19             if (flag == 1 && (ret > preMax || (ret == preMax && *p >= preMod)))
20                 return INT_MAX;
21             if (flag == -1 && (ret > preMax || (ret == preMax && *p > preMod)))
22                 return INT_MIN;
23             ret = ret * 10 + (*p) - '0';
24             p++;
25         }
26         return ret * flag;
27     }
28 };

------------------------------2014.09.20更新写法------------------------------------

改了下判断条件,主要注意两个地方,1)乘以10的时候可能会溢出。2)加的时候也可能会溢出。代码如下。

 1 class Solution {
 2 public:
 3     int atoi(const char *str) {
 4         const char* s = str;
 5         while (*s == ' ') ++s;
 6         int sign = 1;
 7         int ret = 0;
 8         if (*s == '-') {
 9             sign = -1;
10             ++s;
11         } else if (*s == '+') {
12             ++s;
13         }
14         while (*s >= '0' && *s <= '9') {
15             int tmp = ret * 10 + sign * ((*s) - '0');
16             if (ret * 10 / 10 != ret || tmp * sign < 0) {
17                 if (sign > 0) {
18                     ret = INT_MAX;
19                 } else {
20                     ret = INT_MIN;
21                 }
22                 break;
23             }
24             ret = tmp;
25             ++s;
26         }
27         return ret;
28     }
29 };
原文地址:https://www.cnblogs.com/flowerkzj/p/3621734.html