码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode——8 String to Integer (atoi)(自定义字符串转整型,如何避开各种奇葩输入)

时间:2015-06-24 11:02:39      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:leetcode   string   java   算法   溢出   

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.

Hide Tags: Math String

解题思路:
题目不难,主要考察对各种输入的综合处理,如空字符串:“”; 多空格:“  123 1 2 1” ;多符号:“+-123” ;多字符:“+abc123”,以及溢出。
返回结果由两部分构成:基数+符号,因此需要将两部分分别求解。
在程序设计初就要针对各种输入进行处理。编程的逻辑思维大致分四步:
(1)空字符串的处理:如果字符串位空返回0即可
(2)空格的处理:使用循环遍历,将指针跳过空格即可
(3)符号的处理:设置一个符号标识sign,遍历时首先遇到的符号为输出结果的符号

(4)数字与溢出的处理。具体看代码

代码如下:

 public static int myAtoi(String str) 
    {
    	int sign=1,base=0,index=0,digit=0;
    	//空字符串的处理
    	if (str.length()==0)
		{
    		return 0;
		}
    	//空格的处理
    	while (str.charAt(index)==' '&&index<str.length())
		{
    		index++;
		}
    	//确定符号
    	if (str.charAt(index)=='+'||str.charAt(index)=='-')
		{
			sign=str.charAt(index)=='+'?1:-1;
			index++;
			if (sign==1&&str.charAt(index)=='-')
			{
				return 0;
			}
			if (sign==-1&&str.charAt(index)=='+')
			{
				return 0;
			}
		}
    	//确定基数
    	for (int i = index; i < str.length(); i++)
		{
    		char ch=str.charAt(i);
			if (ch<'0'||ch>'9')
			{
				break;
			}
			digit = ch - '0';
    		if (ch>='0'&&ch<='9')
			{
    			/*
    			 * 溢出的处理
    			 * (Integer.MAX_VALUE)/10<base,(Integer.MAX_VALUE)/10==base&&Integer.MAX_VALUE %10 <digit
    			 * 分别表示两种会出现溢出的情况,特别是第二种2147483648时
    			 */
				if ((Integer.MAX_VALUE)/10<base||(Integer.MAX_VALUE)/10==base&&Integer.MAX_VALUE %10 <digit)
				{
					if (sign==1)
					{
						return Integer.MAX_VALUE;
					}
					else
					{
						return Integer.MIN_VALUE;
					}
				}
				base=10*base+digit;
			}
		}
    	return base*sign;
    }

leetcode——8 String to Integer (atoi)(自定义字符串转整型,如何避开各种奇葩输入)

标签:leetcode   string   java   算法   溢出   

原文地址:http://blog.csdn.net/zzc8265020/article/details/46618191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!