# 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.

class Solution {
public:
int myAtoi(string str) {
if(str.size()==0)
return 0;
long long res=0;
int flag=1;
int i=0;
while(str[i]==' ')
i++;
if(str[i]=='+')
i++;
else if(str[i]=='-'){
flag=-1;
i++;
}
for(int j=i;j<str.size();j++)
{
if(str[j]<='9'&&str[j]>='0')
{
res=res*10+(str[j]-'0');
if(res>INT_MAX)
return flag>0?INT_MAX:INT_MIN;
}
else
break;
}
return res*flag;
}
};

#### LeetCode（8）String to Integer (atoi)

2015-04-27 16:57:18

#### Leetcode-8-String to Integer(atoi) C++

2016-06-27 14:24:44

#### [JAVA]LeetCode8 String to Integer (atoi)

2015-04-15 16:09:20

#### LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)

2015-05-07 10:38:53

#### 【LeetCode】LeetCode——第8题：String to Integer (atoi)

2016-04-19 10:26:12

#### [leetcode-8]String to Integer (atoi)(java)

2015-07-23 20:40:00

#### LeetCode(8) String To Integer(atoi)

2015-01-08 12:52:36

#### C++版 - Leetcode 8: String to Integer (myAtoi，C库函数atoi模拟) (剑指offer 面试题49) 解题报告

2016-05-03 10:13:27

#### 【LeetCode】String to Integer (atoi) 解题报告

2014-10-27 16:19:38

#### leetcode 8. 字符串转整数 (atoi) python

2018-05-28 19:11:30