首页 > 代码库 > LeetCode String to Integer (atoi)

LeetCode String to Integer (atoi)

String to Integer (atoi)

Total Accepted: 20984 Total Submissions: 145855

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.

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.



题目是很简单,就是输入一个字符串将其转化成int型。

蛋疼的地方在于各种坑爹的条件。

1.去掉空格 ”  123“ 输出123 ”  + 123“ 输出0

2.+-不能共存 return 0

3.存在其他字符要断句  123a3 输出为123

4.关于越界,越上界返回2147483647 下界返回-2147483648


public class Solution {
  public  int atoi(String str) {
        if(str==null||str.length()==0)
            return 0;
        int isOper = 0; //正负符号,一开始只有0个
        boolean isNeg=false;
        boolean existNumber=false;//判断空格前是否有数字,如:"  +0 123" 
        String a="";

        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='-'||str.charAt(i)=='+'){
                isOper++;//找到一个+/- 符号
                if(isOper>1)
                	return 0;
                if(str.charAt(i)=='-')
                	isNeg=true;
            }
            
            else if(str.charAt(i)==' '){
            	if(existNumber)//空格前面已经有数字了如 123 23
            		break;
            	if(isOper>0)
            	    return 0;
            }
            
            else if(str.charAt(i)>='0'&&str.charAt(i)<='9')
            {
            	a+=str.charAt(i);
            	existNumber=true;
            }
            else
            	break;
        }
        

        int res=0;
        int k=a.length()-1;
        int digit=1;
        
        while(k>=0)
        {
                      
            res+= (a.charAt(k)-'0')*digit;
            if(res<0||(digit%10!=0&&digit!=1)) // res越界直接小于0,digit越界取余不再等于0
            {
                if(isNeg)
                    return -2147483648;
                    else return 2147483647;
            }
            digit*=10;
            k-=1;
        }

        if(isNeg)
            res=-res;
            return  res;  
        
  }
}


LeetCode String to Integer (atoi)