Simple Java Solution :)


#1

public int atoi(final String A) {
String str = A;
int numSeen = 0;
int signSeen = 0,start=0,end=0;
int n = str.length();
String res = “”;
for(int i=0;i<n;i++){
char ch = str.charAt(i);
if(Character.isDigit(ch)){
numSeen = 1;
end++;
}else if(ch == ‘-’ || ch == ‘+’){
if(i != 0) return 0;
start++;signSeen = 1;
}else if(numSeen == 0) {
return 0;
}else{
break;
}
}
if(signSeen == 1){
if(str.charAt(0) == ‘-’)
res = str.substring(0,end+1);
else
res = str.substring(1,end+1);
}else if(end != n){
res = str.substring(start,end);
}else{
res = str;
}
try{
int x = Integer.parseInt(res);
return x;
}catch(Exception e){
if(str.charAt(0) == ‘-’)
return Integer.MIN_VALUE;
else
return Integer.MAX_VALUE;
}
}