Solution:
class Solution {
public:
int atoi(const char *string) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (string == NULL)
return 0;
int num = 0;
int minus = 0;
int continuous = 0, characHasCome = 0;
while ((*string) != '\0') {
if (*string < '0' || *string > '9') {
if (continuous == 1)
break;
if (*string >= 'a' && *string <= 'z')
characHasCome = 1;
if (*string == '-')
minus = 1;
if (*string != ' ')
continuous = 1;
string++;
continue;
}
if (characHasCome == 1)
break;
continuous = 1;
if ((INT_MAX / 10) < num)
if (minus)
return INT_MIN;
else
return INT_MAX;
else if ((INT_MAX / 10) == num) {
if (minus == 0 && (*string - '0') > 7)
return INT_MAX;
else if (minus == 1 && (*string - '0') > 8)
return INT_MIN;
}
num = (num * 10 ) + ((*string) - '0');
string++;
}
if (minus)
return num * (-1);
else
return num;
}
};
===== Second Time ====
class Solution {
public:
int myAtoi(string str) {
int size = str.size();
int num = 0, sign = 1;
bool num_start = false;
if (size == 0)
return 0;
for (int i = 0; i < size; i++) {
if (num == 0 && str[i] == ' ' && !num_start)
continue;
else if (num == 0 && (str[i] == '-' || str[i] == '+') && !num_start) {
sign = (str[i] == '-' ? -1: 1);
num_start = true;
continue;
}
else if (str[i] < '0' || str[i] > '9')
return sign * num;
else if (sign == 1 && num > INT_MAX/10)
return INT_MAX;
else if (sign == 1 && num == INT_MAX/10 && ((str[i] - '0') >= 7))
return INT_MAX;
else if (sign == -1 && -num < INT_MIN/10)
return INT_MIN;
else if (sign == -1 && -num == INT_MIN/10 && ((str[i] - '0') >= 8))
return INT_MIN;
num = (10*num) + (str[i] - '0');
}
return sign * num;
}
};
class Solution {
public:
int atoi(const char *string) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (string == NULL)
return 0;
int num = 0;
int minus = 0;
int continuous = 0, characHasCome = 0;
while ((*string) != '\0') {
if (*string < '0' || *string > '9') {
if (continuous == 1)
break;
if (*string >= 'a' && *string <= 'z')
characHasCome = 1;
if (*string == '-')
minus = 1;
if (*string != ' ')
continuous = 1;
string++;
continue;
}
if (characHasCome == 1)
break;
continuous = 1;
if ((INT_MAX / 10) < num)
if (minus)
return INT_MIN;
else
return INT_MAX;
else if ((INT_MAX / 10) == num) {
if (minus == 0 && (*string - '0') > 7)
return INT_MAX;
else if (minus == 1 && (*string - '0') > 8)
return INT_MIN;
}
num = (num * 10 ) + ((*string) - '0');
string++;
}
if (minus)
return num * (-1);
else
return num;
}
};
===== Second Time ====
class Solution {
public:
int myAtoi(string str) {
int size = str.size();
int num = 0, sign = 1;
bool num_start = false;
if (size == 0)
return 0;
for (int i = 0; i < size; i++) {
if (num == 0 && str[i] == ' ' && !num_start)
continue;
else if (num == 0 && (str[i] == '-' || str[i] == '+') && !num_start) {
sign = (str[i] == '-' ? -1: 1);
num_start = true;
continue;
}
else if (str[i] < '0' || str[i] > '9')
return sign * num;
else if (sign == 1 && num > INT_MAX/10)
return INT_MAX;
else if (sign == 1 && num == INT_MAX/10 && ((str[i] - '0') >= 7))
return INT_MAX;
else if (sign == -1 && -num < INT_MIN/10)
return INT_MIN;
else if (sign == -1 && -num == INT_MIN/10 && ((str[i] - '0') >= 8))
return INT_MIN;
num = (10*num) + (str[i] - '0');
}
return sign * num;
}
};
No comments:
Post a Comment