import java.util.*;
public class Solution {
/**
*
* @param str string字符串
* @return int整型
*/
public int atoi (String str) {
// write code here
if(str.length()==0) return 0; // 如果字符串为空,返回0;
char[] ch = str.toCharArray();
int val = 0;
int k = 0;
int i = 0;
while(ch[i]==' ') i++; //去除头部空格;
for(;i<ch.length;i++){
if(ch[i]>='0'&&ch[i]<='9'&&(k!=-1)){
val = val*10+ch[i]-'0'; //加号或无符号场景
k=1;
}
else if(ch[i]>='0'&&ch[i]<='9'&&(k==-1)){
val = val*10-(ch[i]-'0'); //减号场景
}
else if(ch[i]=='+' && k==0) k=1; //识别加号或减号,只能在头部出现一次
else if(ch[i]=='-' && k==0) k=-1;
else return val;
}
return val;
}
}