当前位置:网站首页>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/
边栏推荐
- Leetcode:829. 连续整数求和
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- 详解Spark运行模式(local+standalone+yarn)
- Ultimate dolls 2.0 | encapsulation of cloud native delivery
- ECMAScript 6.0
- Review column - message queue
- 4. [WebGIS practice] software operation chapter - data import and processing
- ES6解构语法详解
- 72. 编辑距离
- Valid brackets (force deduction 20)
猜你喜欢
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Cookie&Session
在线公网安备案保姆级教程【伸手党福利】
LeetCode 128最长连续序列(哈希set)
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Gorilla/mux framework (RK boot): RPC error code design
Avalanche problem and the use of sentinel
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
Addition without addition, subtraction, multiplication and division
复习专栏之---消息队列
随机推荐
二叉树神级遍历:Morris遍历
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
[reach out to Party welfare] developer reload system sequence
Design of serial port receiving data scheme
pytorch nn. AdaptiveAvgPool2d(1)
leetcode 1482 猜猜看啊,这道题目怎么二分?
Pytorch training deep learning network settings CUDA specified GPU visible
Pathmeasure implements loading animation
Promise中finally的用法
Md5sum operation
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
MFC窗口滚动条用法
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
Leetcode:829. 连续整数求和
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Introduction to EtherCAT
Addition without addition, subtraction, multiplication and division
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
Explain spark operation mode in detail (local+standalone+yarn)