当前位置:网站首页>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/
边栏推荐
- 衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
- 深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
- Pyramid scene parsing network [pspnet] thesis reading
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- Error: plug ins declaring extensions or extension points must set the singleton directive to true
- 10. 正则表达式匹配
- bootsrap中的栅格系统
- 过滤器 Filter
- Leetcode 1482 guess, how about this question?
- multiple linear regression
猜你喜欢
随机推荐
TEC: Knowledge Graph Embedding with Triple Context
Data exchange JSON
Leetcode 128 longest continuous sequence (hash set)
Test function in pychram
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
串口接收数据方案设计
数据交换 JSON
Appium自动化测试基础 — APPium基本原理
md5sum操作
Depth first traversal of C implementation Diagram -- non recursive code
Thread data sharing and security -threadlocal
Ouc2021 autumn - Software Engineering - end of term (recall version)
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
二叉树神级遍历:Morris遍历
Ultimate dolls 2.0 | encapsulation of cloud native delivery
[daily training] 1175 Prime permutation
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
Introduction to EtherCAT
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs









