topic
Atoi you to implement a function, it can convert a string to an integer.
First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.
When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.
In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.
Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.
In any case, if the function can not effectively convert, 0 is returned.
Description:
We assume that the environment can store 32-bit signed integer size, then the numerical range [-231 231--1]. If the value exceeds this range, qing return INT_MAX (231 - 1) or INT_MIN (-231).
Example 1:
Input: "42"
Output: 42
Example 2:
Input: "-42"
Output: -42
Explanation: a first non-blank character '-', it is a negative sign.
We will all digital consecutive negative number and later combined as much as possible, and finally get -42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: converting the digital OFF '3', because the next character is not numeric.
Example 4:
Input: "words and 987"
Output: 0
Explanation: a first non-blank character 'w', but it is not a positive number or negative number.
Therefore, the conversion can not be performed effectively.
Example 5:
Input: "-91283472332"
Output: -2147483648
explanation: the number "-91283472332" Over the range of 32-bit signed integer.
So return INT_MIN (-231).
Thinking
After each string to determine what characters are in line with pre-existing conditions, waiting for the cycle is completed, first determine whether the overflow, if not for output, any information corresponding to the returns.
Note:
1, when the overflow judgment, can not take the integer direct comparison, since has overflowed, and can not be converted.
2 + 0 specific circumstances at the beginning of the note handle
3, beginning with - + 0 should be returned instead of continuing to convert
4, a numeral or - after the number represents the conversion began, experiencing other character represents the end of conversion.
Code
public class T008 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println( myAtoi( " -11919730356x" ) );
}
public static int myAtoi(String str) {
//暂时以字符串形式存储应该返回的数字
String output = "";
int i = 0;
//开始转换的标记
boolean start = false;
while( i<str.length() ) {
//用来暂存每一个字符,用于接下来的判断
char temp = str.charAt( i );
//判断是否为数字,是就进行相应的操作,并将开始转换标记设置为真
if( temp >= 48 && temp <= 57 ){
//判断字符串开头是否为0(忽略-号),有0就不进行存储了
if( temp == 48 && (output.length()==0 || (output.equals( "-" ) ) ) ) {
start = true;
i++;
}else {
start = true;
output += (temp + "");
i++;
}
//判断是否为负号
}else if( temp == '-' ) {
//当负号是除空格外的字符串的第一个,就进行存储,并将开始转变标记设置为真,否则,就跳出循环
if( !start ) {
output += (temp + "" );
start = true;
i++;
}else
break;
//判断是否为空格,是就进行下一步操作
}else if( temp == ' ' ) {
//如果还没开始转换就,下一个字符串,否则跳出循环
if( !start )
i++;
else
break;
//判断是否为+号
}else if( temp == '+' ) {
//是并且还没开始转换,就开始进行转换,但,并不存储正号,否则跳出循环
if( !start ) {
start = true;
i++;
}else
break;
}else
break;
}
//System.out.println( output );
//判断是否越界了
//当字符串长度大于11的时候无论是正还是负,均已经越界,等于11的时候正数越界,负数(因为有负号,实际数字长度为10)再进行下一步判断
if( output.length() >= 11 ) {
//判断是正还是负,正就直接返回越界的信息,负再进行下一步判断
if( output.charAt(0) == '-' ) {
//判断字符串长度是否等于11是就进行下一步判断,否则返回越界信息
if( output.length() == 11 ) {
//当第一位数字大于2的时候,必定越界,等于2再判断,等于1不越界
if( output.charAt(1) > 50 ) {
return Integer.MIN_VALUE;
}else if( output.charAt(1) == 50 ) {
//等于2的时候再对接下来的几位进行判断,这个时候可以去掉一位可以直接转换为整型数判断大小
//大于147483648越界,否则不越界
if( Integer.parseInt( output.substring(2)) > 147483648 )
return Integer.MIN_VALUE;
}
}else
return Integer.MIN_VALUE;
}else
return Integer.MAX_VALUE;
//长度为10的时候负数必定不越界,正数进行判断,方法同上
}else if( output.length() == 10 ) {
if( output.charAt(0) != '-' ) {
if( output.charAt(0) > 50 ) {
return Integer.MAX_VALUE;
}else if( output.charAt(0) == 50 ) {
if( Integer.parseInt( output.substring(1)) >= 147483647 )
return Integer.MAX_VALUE;
}
}
}
//System.out.println( output );
if( output != "" && !output.equals("-") )
return Integer.parseInt(output);
else
return 0;
}
}