LeetCode【8】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.

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

下笔直接就写,结果虎头蛇尾。反复看了题目才终于搞定。上代码,已AC。

int StrToInt(string str)
{
	bool minus = false;
	int sum=0;
	if(str.size()==0)
	{
		return 0;
	}
	for(int i =0;i<str.size();i++)
	{
		//是空格吗?
		if(str[i]==‘ ‘)
			continue;
		//是-吗,那后面是数字吗?
		if(str[i]==‘-‘)
		{
			if(str[i+1]<=‘9‘&& str[i+1]>=‘0‘)
				minus = true;
			else
				break;
		}
		//是+吗,那后面是数字吗?
		else if(str[i]==‘+‘)
		{
			if(str[i+1]>‘9‘||str[i+1]<‘0‘)
				break;
		}
		//是数字吗?
		else if(str[i]<=‘9‘&&str[i]>=‘0‘)
		{
			sum = sum*10+static_cast<int>(str[i]-‘0‘);
			if(str[i+1]>‘9‘||str[i+1]<‘0‘)
				break;
			if(sum>INT_MAX/10 )
			{
				if(minus)
					return INT_MIN;
				else
					return INT_MAX;
			}
			if(sum == INT_MAX/10)
			{
				if(str[i+1]>=‘7‘ && minus==false)
					return INT_MAX;
				if(str[i+1]>=‘8‘ && minus==true)
					return INT_MIN;
			}
		}
		//都不是啊,那就返回咯
		else
			break;
	}
	return minus?(-1*sum):sum;
}

看看别人写的。

真是无语了。直接给链接:http://ask.julyedu.com/question/85

小小作个弊:

int atoi(const char *str) {
        stringstream s;   // 字符串流s
        int a;
        s << str;            // 把str写入流s
        s >> a;              // 从流s中读int
        return a;
}
时间: 2024-12-28 17:00:04

LeetCode【8】string to integer(atoi)的相关文章

LeetCode【8】. String to Integer (atoi) --java实现

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 f

【LeedCode】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 spe

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【leetcode刷题笔记】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 spe

【leetcode】String to Integer (atoi)

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 f

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

Kotlin实现LeetCode算法题之String to Integer (atoi)

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

【LeetCode】String to Integer (atoi) 解题报告

这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] 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 pos

【LeetCode】String to Integer (atoi) 解题报告 (Java)

这道题在LeetCode OJ上难道属于Easy,但是通过率却比较低,究其原因是需要考虑的情况比较低,很少有人一遍过吧. [题目] 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 poss