LeetCode8: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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

一、题目描述

将string类型的字符串转换成整型数据,类似于C++库里的atoi函数

二、解题思路

1. 字串为空或者全是空格,返回0; 

2. 字串的前缀空格需要忽略掉;

3. 忽略掉前缀空格后,遇到的第一个字符,如果是‘+’或‘-’号,继续往后读;如果是数字,则开始处理数字;如果不是前面的2种,返回0;

4. 处理数字的过程中,如果之后的字符非数字,就停止转换,返回当前值;

5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

class Solution {
public:
	int atoi(string str) {

		if (str.length() == 0)
			return 0;
		long long result = 0; //result用于存储结果
		int flag = 1, i = 0;

		while (str[i] == ' ')
		{
			if (str[i] == ' ')
				i++;
		} //字串的前缀空格需要忽略掉

		if (str[i] == '+')
			i++;
		else if (str[i] == '-')
		{
			flag = -1;
			i++;
		}//忽略掉前缀空格后,遇到的第一个字符,如果是‘+’或‘-’号,继续往后

		for (int j = i; j<str.length(); j++)
		{
			if (str[j] >= '0' && str[j] <= '9')
			{
				result = result * 10 + (str[j] - '0');//整数转化为字符加'0',字符转化为整数减‘0'
				if (result > INT_MAX)
					return flag<0 ? INT_MIN : INT_MAX; //转换出的值超出了int型的范围,就返回int的最大值或最小值
			}
			else
				break;  //处理数字的过程中,如果之后的字符非数字,就停止转换,返回当前值
		}
		result *= flag;
		return (int)result;
	}
};

int main()
{
	string s1 = "-2a345sss";
	Solution sol;
	int t = sol.atoi(s1);
	cout << t << endl;
	system("pause");
	return 0;
}
LeetCode8:String to Integer (atoi)_第1张图片

LeetCode8:String to Integer (atoi)_第2张图片






你可能感兴趣的:(LeetCode8:String to Integer (atoi))