Leetcode: 8. String to Integer (atoi)
8. String to Integer (atoi)
题目: Leetcode:8. String to Integer (atoi)
描述:
[blockquote]
- 内容:Implement atoi to convert a string to an integer.
- 题意:字符串转整数,实现一个atoi函数的功能。
[/blockquote]
分析:
[blockquote]
- NO8.String to Integer (atoi) atoi功能详述:(MSDN描述、百度百科描述) 1、跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( )函数来检测); 2、直到遇上数字或正负符号才开始做转换; 3、再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回; 4、如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回 0;
- 思路: 1、遍历字符串依次判断字符。 2、跳过未检出数字的字符的前置空格缩进符号等; 3、检测是否存在数字正负号问题,正负号有且只能被检测到一次; 4、检测到非数字或字符串检测结束时,返回结果; 5、若字符串为空或不能转换成int时,返回0; 6、 如果正确的值超出可表示值的范围,则返回INT_MAX(2147483647)或INT_MIN(-2147483648)
[/blockquote]
代码:
int myAtoi(string str) {
long long llValue = 0;
bool bNeg = false;
int nIndex = 0;
int nLen = str.size();
if (0 == nLen)
{
return 0;
}
while (isspace(str[nIndex]))
{
++nIndex;
}
if (nIndex < nLen
&& ('-' == str[nIndex] || '+' == str[nIndex]))
{
bNeg = '-' == str[nIndex];
++nIndex;
}
if (nIndex < nLen
&& str[nIndex] >= '0'
&& str[nIndex] <= '9')
{
llValue = str[nIndex] - '0';
++nIndex;
while (nIndex < nLen
&& str[nIndex] >= '0'
&& str[nIndex] <= '9')
{
llValue = llValue * 10 + (str[nIndex] - '0');
++nIndex;
if (llValue > INT_MAX)
{
return bNeg ? INT_MIN : INT_MAX;
}
}
return bNeg ? -llValue : llValue;
}
return 0;
} 备注:
[blockquote]
- LC上面给出的要求是 The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. 尽可能多的去掉空格。 但是在MSDN上atoi上以及百度百科atoi上均给出的是 A whitespace consists of space or tab characters 包含已忽略的空格或制表符,百度百科推荐使用isspace。 关于long long来存储越界数字的问题常见Leetcode:7. Reverse Integer
[/blockquote]