import java.util.*;
public class Solution {
/**
*
* @param str string字符串
* @return int整型
*/
public int atoi (String str) {
// write code here
if(str.isEmpty()){
return 0;
}
str=str.replaceAll(" ","");//替换空格
ArrayList list=new ArrayList();
int flag=1;//作为+-判断的依据,默认为+,(1代表+,0代表—)
if(str.charAt(0)=='-'){
flag=0;
}
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='0'&& str.charAt(i)<='9'){//若果是数字,全部添加到list
list.add(Character.toString((str.charAt(i))));//将char转为String
}else if(i>0){//一但发现其他字符,直接退出
break;
}
}
String res="";
for(Object i:list){
res+=(String)i;//拼接字符
}
if(res.length()>10){//此处注意parseInte最长为10位
if(flag==1){
return Integer.MAX_VALUE;
}else{
return Integer.MIN_VALUE;
}
}
if(flag==1){
if(Integer.parseInt(res)>=Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}else{
return Integer.parseInt(res);
}
}else{
if(-Integer.parseInt(res)<=Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}else{
return -Integer.parseInt(res);
}
}
}
}