当前位置:网站首页>Leetcode String to integer(Atoi)
Leetcode String to integer(Atoi)
2022-06-10 16:33:00 【人工智能曾小健】
^:匹配字符串开头
[\+\-]:代表一个+字符或-字符
?:前面一个字符可有可无
\d:一个数字
+:前面一个字符的一个或多个
\D:一个非数字字符
*:前面一个字符的0个或多个
class Solution:
def myAtoi(self, s: str) -> int:
return max(min(int(*re.findall('^[\+\-]?\d+', s.lstrip())), 2**31 - 1), -2**31)
import re
class Solution:
def myAtoi(self, str: str) -> int:
INT_MAX = 2147483647
INT_MIN = -2147483648
str = str.lstrip() #清除左边多余的空格
num_re = re.compile(r'^[\+\-]?\d+') #设置正则规则
num = num_re.findall(str) #查找匹配的内容
num = int(*num) #由于返回的是个列表,解包并且转换成整数
return max(min(num,INT_MAX),INT_MIN) #返回值import re
class Solution:
def myAtoi(self,str:str) -> int:
INT_MAX = 2**31-1
INT_MIN = -2**31
str = str.lstrip()边栏推荐
- Facebook AI | learning reverse folding from millions of prediction structures
- C#_串口通信项目
- VBA determines whether a long string contains another short string
- AIChE | 集成数学规划方法和深度学习模型的从头药物设计框架
- Primekg: building a knowledge map to achieve precision medicine
- 厉害了,工信部推出 “一键解绑” 手机号绑定的互联网账号,堪称神器
- A few misunderstandings about programmers are very harmful!
- 谁在使用我的服务器?在干什么?什么时候?
- 创建 Visual Studio 离线安装包并安装
- 【玩转华为云】手把手带你使用鲲鹏代码迁移工具实现源码迁移
猜你喜欢

See how advanced technology changes human life

重庆第一个科创板IPO,来了

Docker安装Redis镜像详细步骤(简单易懂,适合新手快速上手)

Numpy learning notes

善用产业链招商,打造产业集群效应,实现产业协同发展

新思科技在《 2022 年 Gartner 应用安全测试关键能力报告》中表现优异 在五个常见用例中荣获最高分

Full link tracking & performance monitoring tool skywalking practice

Hidden Markov model and its training (1)

线上交流丨技能网络:解决多任务多模态问题的稀疏模型(青源Talk第19期 唐都钰)

B站不想成为“良心版爱优腾”
随机推荐
Fail fast and fail safe
Simple site backup shell script
Example analysis of SQL injection error reporting
线上交流丨技能网络:解决多任务多模态问题的稀疏模型(青源Talk第19期 唐都钰)
Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
KDD 2021 | MoCL:利用多层次领域知识的分子图对比学习
SOA architecture / test phase interface description language transformation scheme
Brands are difficult to establish, IPO is difficult, and Chinese tea enterprises are trapped in "tradition"?
目标客户匹配数据表格成功案例展示
自定义视图:图形与图像的处理(一):使用简单图片
MFC basic knowledge and course design ideas
Hidden Markov model and its training (1)
Under the "plastic ban order", does the Hong Kong stock exchange pay for the deep excavation of degradable plastics by Zhongbao new materials?
Do you know the five GoLand shortcuts to improve efficiency?
Fabric.js 居中元素 ️
消除业务代码中if....else的五种方式
ADB is not an internal or external command, nor is it a runnable program or batch file
VBA divides strings, stores them in an array, writes them to a file, and saves them
Web3 is the most complete money making secret script. Just read this one
Cannot locate a 64-bit Oracle Client library:The specified module could not be found.