您的位置:首页 > 编程语言 > C语言/C++

LeetCode 008 StringToInteger(atoi)

2016-12-20 17:12 246 查看
[align=left]8. String to Integer (atoi)[/align]

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.
spoilers alert... click to show requirements
for atoi.

[align=left]
[/align]
class Solution {
public:
int myAtoi(string str) {
}
};


[align=left]
[/align]
[align=left]解题思路:[/align]

[align=justify]自己的解题思路[/align]

很简单,就是依次转存。但是,我测试不通过了4次,只考虑string为空的情况,其他情况都没有考虑。可见,对于程序的健壮性这点,以及程序测试这块,我的水平很差,需要继续不断注意训练。

[align=justify]别人的解题思路[/align]

“we now need to compute number = number * base + digval, but
we need to know if overflow occured.  This requires a tricky pre-check. “

程序1 
运用一个与头文件类似的技巧,就是提前检查。比较好。而且使用INT_MAX,具有可移植性。

程序2 
有点类似我的想法。
[align=left]学习收获:[/align]

[align=justify]还是一个对于程序的健壮性考虑。由于题目练得比较少,所以这方面比较薄弱。[/align]

[align=justify]第一次,没有考虑正负号的处理;[/align]

[align=justify]第二次,没有考虑空格的处理;[/align]

[align=justify]第三次,没有考虑越界的情况;所以用了long进行处理[/align]

第四次,连long也存不下了,所以必须提前对程序进行判断。

[align=justify]string format: [whitespace] [sign] [0] [x] [digits/letters][/align]

官方默认有效的string格式是这样的。如果,出现“+-11”,应该返回0;

[align=justify]熟悉使用str.find_first_not_of();[/align]

[align=justify]附件:程序[/align]
[align=justify]1、自己的程序:[/align]
class Solution
{
public:
int myAtoi(string str)
{
long long res = 0;
if(0 == str.size())
{
return res;
}
auto i = str.begin();
while(i != str.end() && isspace(*i))
{
++i;
}
if(i == str.end())
{
return res;
}
bool flag = true;
int sign = 1;
for(; i != str.end(); ++i)
{
if(flag&&*i == '-')
{
sign = -1;
flag = false;
continue;
}
else if(flag&&*i == '+')
{
flag = false;
continue;
}
if(*i >= '0'&&*i <= '9')
{
res = res * 10 + (*i - '0');
}
else
{
break;
}
if(res > INT_MAX)
{
break;
}
}
res = sign*res;
if(res > INT_MAX)
{
res = INT_MAX;
}
else if(res < INT_MIN)
{
res = INT_MIN;
}
return static_cast<int>(res);
}
};


[align=justify]2、别人的程序[/align]
[align=justify]这是比较接近官方文件的解答。[/align]
[align=justify]
[/align]
int myAtoi(string str)
{
int ret = 0, sign = 1, i = str.find_first_not_of(' '), base = INT_MAX / 10;
if(str[i] == '+' || str[i] == '-') sign = str[i++] == '+' ?: -1;
while(isdigit(str[i]))
{
if(ret > base || (ret == base && str[i] - '0' > INT_MAX % 10))          return sign > 0 ? INT_MAX : INT_MIN;
ret = 10 * ret + (str[i++] - '0');
}
return sign * ret;
}
int myAtoi(string str)
{
long result = 0;
int indicator = 1;
for(int i = 0; i < str.size();)
{
i = str.find_first_not_of(' ');
if(str[i] == '-' || str[i] == '+')
indicator = (str[i++] == '-')? -1 : 1;
while('0' <= str[i] && str[i] <= '9')
{
result = result * 10 + (str[i++] - '0');
if(result*indicator >= INT_MAX) return INT_MAX;
if(result*indicator <= INT_MIN) return INT_MIN;
}
return result*indicator;
}
}


[align=justify]
[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Leetcode C++ stoi c语言