LeetCode 8. String to Integer (atoi)

Posted 皇家大鹏鹏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 8. 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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

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这样一个函数,标准库中关于这个函数有详细介绍http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi

根据题目中的描述,我们需要做以下几件事情:

  1. 先跳过一开始的空格字符串
  2. 进行符号的判决,如果一开始读到的字符就是非法字符,直接返回0
  3. 开始读取数字,如果读到非法字符,直接输出当前数字,这个过程中注意处理溢出,如果超过最大值,返回INT_MAX,小于最小值,返回INT_MIN;
  4. 最后把基数与符号相乘,返回结果

代码如下:(写的时候注意,有几个地方涉及到字符串的比较,比如 str[i] - ‘0‘ > 7 不要写成str[i] > 7)

 1 class Solution {
 2 public:
 3     int myAtoi(string str)
 4     { 
 5        int sign = 1, base = 0, i = 0, len = str.size();
 6         while (i < len && str[i] ==  )
 7             i++;
 8         if(str[i] == + || str[i] == -)
 9             sign = (str[i++] == + )? 1:-1;
10         while (i < len && str[i] >= 0 && str[i] <= 9)
11         {
12             if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - 0 > 7))
13             {
14                 if (sign == 1)
15                     return INT_MAX;
16                 else 
17                     return INT_MIN;
18             }
19              base = base * 10 + (str[i++] - 0);   
20         }
21         return base * sign;
22     }
23 };

 


以上是关于LeetCode 8. String to Integer (atoi)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode:String to Integer (atoi)

8. 字符串转整数(实现atoi函数) [leetcode 8: String to Integer (atoi)]

8. 字符串转整数(实现atoi函数) [leetcode 8: String to Integer (atoi)]

leetcode-8. String to Integer (atoi)

Leetcode 8. String to Integer (atoi)

[LeetCode] 8. String to Integer (atoi) ☆