当前位置:网站首页>8. 字符串转换整数 (atoi)
8. 字符串转换整数 (atoi)
2022-07-01 03:23:00 【Sun_Sky_Sea】
8. 字符串转换整数 (atoi)
原始题目链接:https://leetcode.cn/problems/string-to-integer-atoi/
请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
函数 myAtoi(string s) 的算法如下:
读入字符串并丢弃无用的前导空格
检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
将前面步骤读入的这些数字转换为整数(即,“123” -> 123, “0032” -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
返回整数作为最终结果。
注意:
本题中的空白字符只包括空格字符 ’ ’ 。
除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
示例 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)、’ ‘、’+‘、’-’ 和 ‘.’ 组成
解题思路:
使用正则表达式,或者使用状态自动机算法。
代码实现:
正则法:
import re
class Solution:
def myAtoi(self, s: str) -> int:
# 正则匹配
matches = re.match('[ ]*([+-]?\d+)', s)
if not matches:
return 0
# 返回匹配的内容,正则命中的内容都在1号捕获组
res=int(matches.group(1))
return min(max(res, -2**31), 2**31-1)
参考文献:
https://leetcode.cn/problems/string-to-integer-atoi/solution/xiong-mao-shua-ti-python3-yi-qi-xue-xi-zheng-ze-bi/
边栏推荐
猜你喜欢

Depth first traversal of C implementation Diagram -- non recursive code
![[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page

Keil5中如何做到 0 Error(s), 0 Warning(s).

Ultimate dolls 2.0 | encapsulation of cloud native delivery

小程序容器技术与物联网IoT的结合点

Edge drawing: a combined real-time edge and segment detector

在线公网安备案保姆级教程【伸手党福利】

File upload and download
随机推荐
4、【WebGIS实战】软件操作篇——数据导入及处理
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
Appium自动化测试基础 — APPium基本原理
Develop industrial Internet with the technical advantages of small programs
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
多元线性回归
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
[party benefits] jsonobject to string, leave blank
jeecgboot输出日志,@Slf4j的使用方法
File upload and download
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Pyramid Scene Parsing Network【PSPNet】论文阅读
Complete knapsack problem
GCC usage, makefile summary
leetcode 1818 绝对值,排序,二分法,最大值
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Addition without addition, subtraction, multiplication and division
Listener listener
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表