class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (str == NULL) {
return 0;
}
bool isNegtive = false;
char* start = const_cast<char*>( str);
int len = strlen(str);
char* end = start+len-1;
if (*start == '-') {
isNegtive = true;
start++;
}
if (*start=='+') {
isNegtive = false;
start++;
}
int res = 0;
bool first = true;
while (start <= end) {
if (first&&*start=='0') {
start++;
}
if (isdigit(*start)) {
first = false;
res = res*10+(*start-'0');
}
start++;
}
if (isNegtive) {
res = 0-res;
}
return res;
}
};
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (str == NULL) {
return 0;
}
bool isNegtive = false;
char* start = const_cast<char*>( str);
int len = strlen(str);
char* end = start+len-1;
if (*start == '-') {
isNegtive = true;
start++;
}
if (*start=='+') {
isNegtive = false;
start++;
}
int res = 0;
bool first = true;
while (start <= end) {
if (first&&*start=='0') {
start++;
}
if (isdigit(*start)) {
first = false;
res = res*10+(*start-'0');
}
start++;
}
if (isNegtive) {
res = 0-res;
}
return res;
}
};