当前位置:网站首页>【LeetCode】8、字符串转换整数(atoi)
【LeetCode】8、字符串转换整数(atoi)
2022-06-21 20:23:00 【小曲同学呀】
8、字符串转换整数(atoi)
请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的atoi 函数)。
函数 myAtoi(string s) 的算法如下:
- 读入字符串并丢弃无用的前导空格
- 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
- 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
- 将前面步骤读入的这些数字转换为整数(即,“123” -> 123, “0032” -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
- 如果整数数超过 32 位有符号整数范围 [−2^31, 2 ^31 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于−2 ^31 的整数应该被固定为 −2 ^31 ,大于 2 ^31 − 1 的整数应该被固定为 2 ^31 − 1 。
- 返回整数作为最终结果。
注意:
本题中的空白字符只包括空格字符 ’ ’ 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
示例1:
输入:s = "42"
输出:42
解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"42"(读入 "42")
^
解析得到整数 42 。
由于 "42" 在范围 [-2^31, 2^31 - 1] 内,最终结果为 42 。
示例 2:
输入:s = " -42"
输出:-42
解释:
第 1 步:" -42"(读入前导空格,但忽视掉)
^
第 2 步:" -42"(读入 '-' 字符,所以结果应该是负数)
^
第 3 步:" -42"(读入 "42")
^
解析得到整数 -42 。
由于 "-42" 在范围 [-2^31, 2^31 - 1] 内,最终结果为 -42 。
示例 3:
输入:s = "4193 with words"
输出:4193
解释:
第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"4193 with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
^
解析得到整数 4193 。
由于 "4193" 在范围 [-231, 231 - 1] 内,最终结果为 4193 。
提示:
0 <= s.length <= 200
s 由英文字母(大写和小写)、数字(0-9)、' '、'+'、'-' 和 '.' 组成
解题思路:
对于本题,看了看网上和官解,都是说在使用状态机自动化,标记状态等等说法。
我觉得没多大必要,首先这个问题其实是没有考察算法知识的,它主要是模拟的日常开发中对于原始数据的处理(例如「参数校验」等场景)。
其实很多时候,业务需求就是类似这样的问题,工作中如果遇到:
- 1、有现成的工具和类库需尽量使用,因为它们是性能更优,且经过更严格测试,是相对可靠的;
- 2、能抽取成工具类、工具方法的尽量抽取,以突出主干逻辑、方便以后代码复用;
- 3、不得不写得比较繁琐、冗长的时候,需要写清楚注释、体现逻辑层次,以便上线以后排查问题和后续维护。
根据题目得出几个要点:
- 根据示例 1,需要去掉前导空格;
- 根据示例 2,需要判断第 1 个字符为 + 和 - 的情况,因此,可以设计一个变量 sign,初始化的时候为 1,如果遇到 - ,将 sign 修正为 -1;
- 判断是否是数字,可以使用字符的 ASCII 码数值进行比较,即 0 <= c <= ‘9’;
- 根据示例 3 和示例 4 ,在遇到第 1 个不是数字的字符的情况下,转换停止,退出循环;
- 根据示例 5,如果转换以后的数字超过了 int 类型的范围,需要截取。这里不能将结果 res 变量设计为 long 类型,注意:由于输入的字符串转换以后也有可能超过 long 类型,因此需要在循环内部就判断是否越界,只要越界就退出循环,这样也可以减少不必要的计算;
- 由于涉及下标访问,因此全程需要考虑数组下标是否越界的情况。
参考代码:
public class Solution {
public int myAtoi(String str) {
int len = str.length();
// str.charAt(i) 方法回去检查下标的合法性,一般先转换成字符数组
char[] charArray = str.toCharArray();
// 1、去除前导空格
int index = 0;
while (index < len && charArray[index] == ' ') {
index++;
}
// 2、如果已经遍历完成(针对极端用例 " ")
if (index == len) {
return 0;
}
// 3、如果出现符号字符,仅第 1 个有效,并记录正负
int sign = 1;
char firstChar = charArray[index];
if (firstChar == '+') {
index++;
} else if (firstChar == '-') {
index++;
sign = -1;
}
// 4、将后续出现的数字字符进行转换
// 不能使用 long 类型,这是题目说的
int res = 0;
while (index < len) {
char currChar = charArray[index];
// 4.1 先判断不合法的情况
if (currChar > '9' || currChar < '0') {
break;
}
// 题目中说:环境只能存储 32 位大小的有符号整数,因此,需要提前判:断乘以 10 以后是否越界
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && (currChar - '0') > Integer.MAX_VALUE % 10)) {
return Integer.MAX_VALUE;
}
if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && (currChar - '0') > -(Integer.MIN_VALUE % 10))) {
return Integer.MIN_VALUE;
}
// 4.2 合法的情况下,才考虑转换,每一步都把符号位乘进去
res = res * 10 + sign * (currChar - '0');
index++;
}
return res;
}
public static void main(String[] args) {
Solution solution = new Solution();
String str = "2147483646";
int res = solution.myAtoi(str);
System.out.println(res);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}

边栏推荐
- solidity实现智能合约教程(4)-ERC1155合约
- Revenue and profit "ebb and flow", water drops turn in pain
- How to search foreign literature efficiently?
- 广东疾控提醒:暑期将至,返粤大学生这样安全“归巢”
- Communication failure between botu simulation HMI and real 1200plc
- 精彩回顾丨一图了解华为云专场干货
- Specificity and application of Worthington Papain
- Go unit test mocks the database CRUD
- LeetCode508-出现次数最多的子树元素和-深搜
- 博图仿真HMI与真实1200PLC通讯失败
猜你喜欢

赋·新生,链·未来!城链科技产业赋能创新发展大会隆重举行

#16迭代器经典案例

自制C#编译器

Six interesting web page effects that can be used

使用StreamAPI 断言组合,结合本地缓存做模糊查询(比mysql效率提升近1000倍)

GAMES101作业7-多线程提速实现步骤详解

Advanced packaging, the beginning of a big cycle -- a symposium on semiconductor equipment

提升方法(上)AdaBoost

六个拿来就能用的有趣网页特效

The order management system realizes the unified management of enterprise order inventory
随机推荐
基于OpenCVSharp的001新建工程项目
五分钟带你了解云原生
Advanced packaging, the beginning of a big cycle -- a symposium on semiconductor equipment
挖财赠送的证券账户安全吗?可以开户吗?
CF1481F AB Tree
Tkinter drawing component (29) -- Radio Group Control
Overriding Overloading final
Communication failure between botu simulation HMI and real 1200plc
Worthington collagenase raw material
As we media, why can some people earn more than 10000 yuan a month, but you can only earn a few yuan a month?
Is the security of the securities account given by digging money safe? Can I open an account?
Specificity and application of Worthington Papain
利用tRNAscan-SE做tRNA分析
B2B mall website helps enterprises speed up distribution and build an efficient and intelligent B2B online distribution platform
Which iPad apps can read English literature well?
dotter|打点法进行序列两两比较软件
北京 加速生态建设 迈动互联与摩尔线程完成产品兼容互认证
CF1481F AB Tree
How to check the duplicate of an English paper?
自制C#编译器