- 题目描述
请你来实现一个 atoi 函数,使其能将字符串转换成整数。
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
在任何情况下,若函数不能进行有效的转换时,请返回 0。
说明:
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,qing返回 INT_MAX (2^31 − 1) 或 INT_MIN (−2^31) 。
示例 1:
输入: "42"
输出: 42
示例 2:
输入: " -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
示例 3:
输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
示例 4:
输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
因此无法执行有效的转换。
示例 5:
输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。
因此返回 INT_MIN (−231) 。
题目解析
这道题目比较长,因为异常情况太多了,整体读完需要获取的信息如下:
- 字符串转为数字,非法输入时返回0
- 数字前面出现“-”则为负数,“+”或者直接数字开头为正数,其他字符开头直接判为非法输入
比如: “ 0=” 输出为 0, “a001" 输出为0,“+-”输出为0,“.1”输出为0
- int为32有符号型,所以输入输出有上限和下限
总结一下就是:将字符串第一个非空连续字符转为int(32bit)输出,未能成功转换输出0
java第二版本实现,简化了逻辑,优化了字符串转数字逻辑,执行实现从4ms变为现在的2ms。
static final int max = Integer.MAX_VALUE/10;
int myAtoi(String str) {
// 非法情况直接返回,节省时间
if (str == null || str.isEmpty()) return 0;
// trim一下,简化逻辑
String trimmed = str.trim();
if (trimmed.isEmpty()) return 0;
// 非空第一个字符,决定正负,确认 起始遍历的index
boolean isPositive = true;
int s = 0, length = trimmed.length();
char c = trimmed.charAt(0);
if (c == '-') {
isPositive = false;
s = 1;
} else if (c == '+') {
s = 1;
}
// str to int
int result = 0, current;
for (int i = s; i < length; i++) {
current = trimmed.charAt(i) - 48;
if (current > 9 || current < 0) break;
if (result > max || (result == max && current > 7)) {
return isPositive ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + current;
}
return isPositive ? result : -result;
}
以下为旧版本实现,看起来比较啰嗦,也没有真正的在运行时支持32为存储,中间用了long型做转换。
/**
* @author yangbixuan
* @param str 输入,肯定非空
* @return 解析结果
*/
public int myAtoi(String str) {
System.out.print("\"" + str + "\" >>>> ");
// 非法情况直接返回,节省时间
if (str == null || str.isEmpty()) {
return 0;
}
// trim一下,简化逻辑
String trimed = str.charAt(0) == ' ' ? str.trim() : str;
if (trimed.isEmpty()) {
return 0;
}
// 非空第一个字符,决定正负,确认 起始遍历的index
Boolean isPositive = null;
int s , length = trimed.length();
char c = trimed.charAt(0);
if (c == '-') {
isPositive = false;
s = 1;
} else if (c == '+') {
isPositive = true;
s = 1;
} else if (c >= '0' && c <= '9') {
isPositive = true;
s = 0;
} else {
return 0;
}
// 开始搜寻连续有效的数字,跳过前缀的0
int e = s;
boolean start = false;
for (int i = s; i < length; i++) {
char ch = trimed.charAt(i);
if (ch >= '0' && ch <= '9' && e - s <= 10) {
if (!start && ch == '0') {
s = i;
e = s + 1;
continue;
}
start = true;
e++;
} else {
break;
}
}
// 解析结果,简单情况直接计算,过长的用long接收并截断
if (s == e || e == 0) {
return 0;
} else if (e - s == 1) {
int result = trimed.charAt(s) - '0';
return isPositive ? result : 0 - result;
}
String num = trimed.substring(s, e);
if (num.length() < 10) {
int result = Integer.parseInt(num);
return isPositive ? result : 0 - result;
} else {
long result = Long.parseLong(num);
if (isPositive) {
return result >= 2147483647L ? Integer.MAX_VALUE : (int)result;
} else {
return result >= 2147483648L ? Integer.MIN_VALUE : (int)(0 - result);
}
}
}
题解:杨比轩
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/string-to-integer-atoi