coding
unsky
deepdim
thought

String to Integer (atoi) 字符转int类型

id8:String to Integer (atoi) QuestionEditorial Solution My Submissions
Total Accepted: 136242
Total Submissions: 989232
Difficulty: Easy
Contributors: Admin
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.

这道题目正常的转换非常简单,比较麻烦的就是处理边界问题,在测试集里面需要考虑如下集中边界问题:

  1. 起始和结束空格问题如 "." return 0;和 "....-48755...." return -48755, 其中 "." 表示空格,
  2. 去掉问题1后的符号字符问题,比如 +8975 return 8975, 89454 return 89484,-8975 return -8975, 即必须提取出合适的正负号.其中正号可以省略。
  3. 去掉1和2问题后的结束字符问题,如,8986jhuuh return 8986.
  4. 溢出问题,写此文章时2016/11/21,leetcode已经在此测试集中加入了对long long的溢出判断。所以不能使用long long 类型进行溢出判断。可以使用
    http://deepdim.com/2016/11/21/Reverse-Integer-%E9%80%86%E7%BD%AEint%E6%95%B0/#more
    中的溢出方法,即:使用前一个结果得标志 在溢出得时候只有每次乘以10的时候会产生溢出,假设没乘以10之前的结果保存在pre_result,在乘以10之后的结果为 result,可以使用result/10!=pre_result来进行溢出判断,因为如果溢出结果就是一个溢出的数字不满足乘以10的结果

    note: 测试集中存在测试字符串 +-8968,在去掉1和2问题之后变为’-8968’等同于".",所以return 0,如果不好理解,可以将问题放大:如-895-775在去掉问题1和2的影响后,等同于895所以return 895.

程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int myAtoi(string str) {
str.erase(0,str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ')+1);
int result=0;
int pre_result;
int sigm=1;
if(str[0]=='-')
{sigm=-1;
str=str.substr(1);
}
else if(str[0]=='+')
str=str.substr(1);
if (str.length()==0)return 0;
for(int i=0;i<str.length();i++)
{if(str[i]<'0'||str[i]>'9')
break;
pre_result=result;
result=result*10+str[i]-48;
if(result/10!=pre_result)//溢出操作,注意在测试数据集里 Long long 类型也溢出
{ if(sigm<0)return INT_MIN;
else return INT_MAX;
}
}
return (sigm*result);
}
};

本地测试用例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
using namespace std;
int INT_MIN=-2147483648;
int INT_MAX=2147483647;
class Solution {
public:
int myAtoi(string str) {
str.erase(0,str.find_first_not_of(' '));
str.erase(str.find_last_not_of(' ')+1);
int result=0;
int pre_result;
int sigm=1;
if(str[0]=='-')
{sigm=-1;
str=str.substr(1);
}
else if(str[0]=='+')
str=str.substr(1);
if (str.length()==0)return 0;
for(int i=0;i<str.length();i++)
{if(str[i]<'0'||str[i]>'9')
break;
pre_result=result;
result=result*10+str[i]-48;
if(result/10!=pre_result)//溢出操作,注意在测试数据集里 Long long 类型也溢出
{ if(sigm<0)return INT_MIN;
else return INT_MAX;
}
}
return (sigm*result);
}
};
int main()
{ string str;
cin>>str;
Solution so;
cout<<so.myAtoi(str);
return 0;
}

坚持原创技术分享,您的支持将鼓励我继续创作!