public static int myAtoi(String str) {
if (str == null  str.length() == 0)
return 0;//
str = str.trim();
char firstChar = str.charAt(0);
int sign = 1, start = 0, len = str.length();
long sum = 0;
if (firstChar == '+') {
sign = 1;
start++;
} else if (firstChar == '') {
sign = 1;
start++;
}
for (int i = start; i < len; i++) {
if (!Character.isDigit(str.charAt(i)))
return (int) sum * sign;
sum = sum * 10 + str.charAt(i)  '0';
if (sign == 1 && sum > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (sign == 1 && (1) * sum < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
}
return (int) sum * sign;
}
JAVAEasy Version To Understand!!!!!!!!!!


@ramunerella I am also interested in knowing how this line of code works. Did you find a solution yet?

I believe this is a lot simpler:
int myAtoi(String str) { try { BigInteger value = new BigInteger(str.replaceFirst("^\\s*([+]?\\d+).*?$", "$1")); return (value.signum() < 0 ? value.max(BigInteger.valueOf(Integer.MIN_VALUE)).intValue() : value.min(BigInteger.valueOf(Integer.MAX_VALUE)).intValue(); } catch (NumberFormatException e) { return 0; } }
note, you'll have to
import java.math.BigInteger

@HelloWorld123456 this code is wrong, try input "15\u0967". I've submitted new test cases to make this code not passing.
