代码为:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Scanner;
public class LeetCode8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.print(myAtoi(str));
}
static int myAtoi(String s) {
Pattern p = Pattern.compile("^-?\\d+$");
Matcher m = p.matcher(s);
long i = 0;
if (m.find()) {
i = Long.valueOf(m.group()).longValue();
}
return i > Integer.MAX_VALUE || i < Integer.MIN_VALUE ? 0 : (int)i;
}
}
力扣上执行到字符串4193 with words返回值为0,我在本地执行是对的