
public class Solution {
public int StrToInt(String str) {
if(str==null||str.length()==0)
return 0;
int count=0;
boolean tag=true;
if(str.charAt(0)!='-'&&str.charAt(0)!='+'&&
(str.charAt(0)<48||str.charAt(0)>57||str.charAt(0)=='0'))
{
return 0;
}
if(str.charAt(0)=='-') //第一个符号为负号
{
tag=false;
}
if(str.charAt(0)!='+'&&tag) //第一个符号为数字
count=(str.charAt(0)-48)*(int)Math.pow(10,str.length()-1);
for(int i=1;i!=str.length();i++)
{
if(str.charAt(i)>=48&&str.charAt(i)<=57)
{
count+=(str.charAt(i)-48)*(int)Math.pow(10,str.length()-i-1);
}else{
return 0;
}
}
if(!tag)
{
count*=-1;
}
return count;
}
public static void main(String[]args){
//System.out.println("Hello");
Solution s=new Solution();
System.out.println(s.StrToInt("+2147483647"));
}
}

还没有评论,来说两句吧...