solution.cpp 577 字节
Newer Older
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 41
int myAtoi(char *str)
{
	int i = 0;
	int sign = 0;
	while (str[i] && str[i] == ' ')
		i++;
	if (str[i] == NULL)
		return 0;
	if (str[i] == '-')
	{
		sign = 1;
		i++;
	}
	else if (str[i] == '+')
	{
		sign = 0;
		i++;
	}
	else if (str[i] < '0')
		return 0;
	else if (str[i] > '9')
		return 0;
	long long int r = 0;
	while (str[i])
	{
		if (str[i] < '0')
			break;
		else if (str[i] > '9')
			break;
		else
			r = r * 10 + str[i++] - '0';
		if (r > INT_MAX)
			break;
	}
	r = sign ? -r : r;
	if (r < INT_MIN)
		return INT_MIN;
	if (r > INT_MAX)
		return INT_MAX;
	return (int)r;
}