当前位置:网站首页>LeetCode 8. 字符串转换整数 (atoi)(中等、字符串)
LeetCode 8. 字符串转换整数 (atoi)(中等、字符串)
2022-06-10 23:31:00 【碳烤小肥羊。。。】
题目描述:请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
函数 myAtoi(string s) 的算法如下:
1. 读入字符串并丢弃无用的前导空格
2. 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
3. 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
4. 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
5. 返回整数作为最终结果。
注意:
本题中的空白字符只包括空格字符 ' ' 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
示例 1:
输入:s = "42"
输出:42
解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
^
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
^
第 3 步:"42"(读入 "42")
^
解析得到整数 42 。
由于 "42" 在范围 [-231, 231 - 1] 内,最终结果为 42 。
示例 2:
输入:s = " -42"
输出:-42
解释:
第 1 步:" -42"(读入前导空格,但忽视掉)
^
第 2 步:" -42"(读入 '-' 字符,所以结果应该是负数)
^
第 3 步:" -42"(读入 "42")
^
解析得到整数 -42 。
由于 "-42" 在范围 [-231, 231 - 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)、' '、'+'、'-' 和 '.' 组成
代码实现:
public static int myAtoi(String s){
if(s.length() == 0){
return 0;
}
int index = 0; // 遍历到当前字符位置
long res = 0; // 最终结果
int sign = 1; // 1是整数, -1是负数
int length = s.length(); // 数组长度
// 逻辑上去掉空格
while (index < length && s.charAt(index) == ' '){
index++;
}
if(index == length){
return 0;
}
// 判断符号
if(s.charAt(index) == '-' || s.charAt(index) == '+'){
sign = s.charAt(index++) == '+' ? 1 : -1;
}
for (; index < length; index++) {
int digit = s.charAt(index) - '0';
if(digit < 0 || digit > 9){
// 不是数字的情况直接退出
break;
}
res = res * 10 + digit;
// 如果超过了最大值,直接返回最大值
if(res * sign > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
// 如果小于最小值,直接返回最小值
if(res * sign < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
return (int) (sign * res);
}
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/string-to-integer-atoi
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
猜你喜欢

Introduction and basic construction of kubernetes

海贼oj#448.抽奖

微信小程序实现OCR扫描识别
![[network planning] 2.2.4 Web cache / proxy server](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.2.4 Web cache / proxy server

双飞翼布局

飞利浦 COO 人事变动,将临危受命解决“供应链和产品召回”双重危机
![[network planning] 2.1.2 transport layer services that can be selected by the application](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.1.2 transport layer services that can be selected by the application

MESI cache consistency protocol for concurrent programming

Objects as points personal summary

USB IP core FPGA debugging (I)
随机推荐
Can I buy annuity insurance? Is annuity insurance safe?
mybaits merge into
飞利浦 COO 人事变动,将临危受命解决“供应链和产品召回”双重危机
Mysql database table backup
[network planning] 3.2 transport layer - UDP: connectionless service
Kubernetes入门介绍与基础搭建
测试下吧先
[network planning] 2.1.2 transport layer services that can be selected by the application
网络工程师必修课防火墙安全区域及安全策略基础操作
JVM garbage collection mechanism and common garbage collectors
[JVM] garbage collection mechanism
The driver has not received any packets from the server
The mystery of number idempotent and perfect square
With a market value of 21.5 billion yuan, will the post-80s generation in Sichuan make TV history?
双飞翼布局
LeetCode 1673. Find the most competitive subsequence**
微信小程序实现OCR扫描识别
Décomposition détaillée du problème de chemin le plus court du graphique
Blocking queue - delayedworkqueue source code analysis
[JVM] class loading mechanism