import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public int myAtoi(String str) {
//清空字符串开头和末尾空格(这是trim方法功能,事实上我们只需清空开头空格)
str = str.trim();
//java正则表达式
Pattern p = Pattern.compile("^[\\+\\-]?\\d+");
Matcher m = p.matcher(str);
int value = 0;
//判断是否能匹配
if (m.find()){
//字符串转整数,溢出
try {
value = Integer.parseInt(str.substring(m.start(), m.end()));
} catch (Exception e){
//由于有的字符串"42"没有正号,所以我们判断'-'
value = str.charAt(0) == '-' ? Integer.MIN_VALUE: Integer.MAX_VALUE;
}
}
return value;
}
}
LeetCode算法题:字符串转换整数 (atoi)
最新推荐文章于 2020-04-25 18:40:34 发布