【leetcode】8.String to Integer(atoi)(Medium)(C)

Description:

Implement atoi which converts a string to an integer.
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.

Note:

Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example1:

Input: “42”
Output: 42

Example2:

Input: " -42"
Output: -42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
Then take as many numerical digits as possible, which gets 42.

Example3:

Input: “4193 with words”
Output: 4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.

Example4:

Input: “words and 987”
Output: 0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.

Example4:

Input: “-91283472332”
Output: -2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.

[ 题目链接 ]

int myAtoi(char* str) {
	//char c;
	int i = 0, j = 0, flag,len=0, tmp = 0,flag3=0;
	//int pos_start, pos_end;
	int nums[11];

	//初始化nums数组
	for (int k = 0; k < 10; k++)
		nums[k] = -1;

	//剔除前面的空格和0
	while (str[i] == ' ')
		i++;
	while (str[i] == '0')
	{
		flag3 = 1;
		i++;
	}

	//判断第一个字符
	//
	if (str[i] - '0' >= 0 && str[i] - '0' <= 9)
		flag = 1;
	else if (str[i] == '+')
	{
		if (flag3 == 1)	return 0;
		flag = 1;
		i++;
	}
	else if (str[i] == '-')
	{
		if (flag3 == 1)	return 0;
		flag = -1;
		i++;
	}
	else
		return 0;

	//再踢一遍0
	while (str[i] == '0')
		i++;

	//将数字存储到数组nums中
	//因为int最大到10位,所以nums数组有11位(包含符号位)
	while (str[i] >= '0'&&str[i] <= '9'&&j < 10)
	{
		nums[j++] = str[i++] - '0';
		len++;
	}

	//录完10个数字之后字符串里面还有数字
	//数字就超过了10位,直接返回int的最大值
	if (str[i] >= '0'&&str[i] <= '9')  // 这里加个=
	{
		if (flag == 1)
			return 2147483647;
		else
			return INT_MIN;
	}

	//如果数字有10位
	if (j == 10)
	{
		//如果最高位大于2 直接返回最大值
		if (nums[0] > 2)
		{
			if (flag == 1)
				return INT_MAX;
			else return INT_MIN;
		}
		//如果最高位是2,且有10位,要进行超额的判断
		else if (nums[0] == 2)
		{
			for (int k = 1; k < 10; k++)
			{
				tmp = tmp * 10 + nums[k];
			}
			if (flag == 1)
			{
				if (tmp > 147483647)
					return 2147483647;
				else return 2000000000 + tmp;
			}
			else
			{
				if (tmp > 147483648)
					return INT_MIN;    //这里不能返回-2147483648
				else return -1 * (2000000000 + tmp);
			}
		}
	}

	//其余的进行正常处理
	tmp = 0;
	for (i = 0; nums[i] != -1&&i<len; i++)
		tmp = tmp * 10 + nums[i];
	return flag * tmp;

}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AXIMI/article/details/83008226