【JDK1.8】
import java.util.*;
import java.math.BigInteger;
public class Solution {
/**
*
* @param str string字符串
* @return int整型
*/
public int atoi (String str) {
String strInt="" ;// write code here
int intRe = 0;
str = str.trim();
for(int i = 0 ; i< str.trim().length();i++){
strInt = strInt + str.charAt(i);
System.out.println(strInt);
if(i== 0 && (str.charAt(i)== '+' || str.charAt(i)== '-' ) ){
continue;
}
try{
if((new BigInteger(strInt)).compareTo(new BigInteger(String.valueOf(Integer.MAX_VALUE))) >0 ) {
intRe = Integer.MAX_VALUE;
}else if( (new BigInteger(strInt)).compareTo(new BigInteger(String.valueOf(Integer.MIN_VALUE))) <0) {
intRe = Integer.MIN_VALUE;
}else{
intRe = Integer.parseInt(strInt);
}
}catch(Exception e){
e.printStackTrace();
}
}
return intRe;
}
}
449

被折叠的 条评论
为什么被折叠?



