#leetcode# String to Integer
题目来自:https://oj.leetcode.com/problems/string-to-integer-atoi/
1、题目简介
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.
spoilers alert... click to show 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.
2、程序实现
代码一:
int atoi(const char* str)
{
int ret = 0;
if(str==NULL)
{
return ret;
}
const char* p = str;
while(p==" ")//error
{
p++;
}
bool minus = false;
if(*p=='-')
{
minus = true;
p++;
}
bool valid = false;
if(*p>='0' && *p<='9')
{
valid = true;
}
if(valid)
{
while(*p>='0' && *p<='9')//error, Loop termination conditions
{
int part = (*p-0);
if((INT_MAX - ret * 10) < part)
{
return INT_MAX;
}
if(minus && (INT_MIN + part) > ret * 10)
{
return INT_MIN;
}
ret = ret * 10 + part;
}
}
if(!minus)
{
ret = -1 * ret;
}
return ret;
}
问题1,函数的输入参数时const类型,为什么声明的非const类型p可以直接指向,并可以执行自增操作。
问题2,字符串char* p,如果取其中一个元素,如何求?
const char* p = str; printf("%d", *p-'0'); 为什么可以这样?什么时候是字符计算,什么时候是字符串计算?
貌似是通过运算符区分的。
问题3,指针没有判断结束。
代码二:
int atoi(const char* str)
{
int ret = 0;
if(str==NULL)
{
return ret;
}
const char* p = str;
while(*p==' ')
{
p++;
}
bool minus = false;
if(*p=='-')
{
minus = true;
p++;
}
bool valid = false;
if(*p >= '0' && *p <= '9')
{
valid = true;
}
if(valid)
{
while(p != NULL && *p >= '0' && *p <= '9')
{
int part = *p - '0';
printf("%d\n", part);
if((INT_MAX - ret * 10) < part)
{
return INT_MAX;
}
if(minus && (INT_MIN + (*p - '0')) > ret * 10)
{
return INT_MIN;
}
ret = ret * 10 + (part - 0);
p++;
}
}
if(minus)
{
ret = -1 * ret;
}
return ret;
}
代码三
int atoi(const char* str)
{
int ret = 0;
if(str==NULL)
{
return ret;
}
const char* p = str;
while(*p==' ')
{
p++;
}
bool minus = false;
if(*p=='-')
{
minus = true;
p++;
}
else if(*p=='+')
{
p++;
}
bool valid = false;
if(*p >= '0' && *p <= '9')
{
valid = true;
}
if(valid)
{
while(p != NULL && *p >= '0' && *p <= '9')
{
int part = *p - '0';
if(minus)
{
//printf("%d\t%d\n", ret, part);
if(INT_MIN / 10 > -1 * ret)
{
return INT_MIN;
}
else
{
if((INT_MIN + (*p - '0')) > ret * -10)
{
return INT_MIN;
}
}
ret = ret * 10 + (part - 0);
}
else
{
if(INT_MAX / 10 < ret)
{
return INT_MAX;
}
if((INT_MAX - ret * 10) < part)
{
return INT_MAX;
}
ret = ret * 10 + (part - 0);
}
p++;
}
}
if(minus)
{
ret = -1 * ret;
}
return ret;
}
3、测试case
-1
10522545459
+1
-2147483648
-2147483649
-11919730356x
浙公网安备 33010602011771号