当前位置:网站首页>8. string conversion integer (ATOI)
8. string conversion integer (ATOI)
2022-07-01 03:42:00 【Sun_ Sky_ Sea】
8. String conversion integers (atoi)
Original title link :https://leetcode.cn/problems/string-to-integer-atoi/
Please come to realize a myAtoi(string s) function , Enable it to convert a string to a 32 Bit signed integer ( similar C/C++ Medium atoi function ).
function myAtoi(string s) The algorithm is as follows :
Read in strings and discard useless leading spaces
Check the next character ( Suppose you haven't reached the end of the character yet ) Positive or negative , Read the character ( If there is ). Determine whether the final result is negative or positive . If neither exists , Suppose the result is positive .
Read in the next character , Until you reach the next non numeric character or the end of the input . The rest of the string will be ignored .
Convert the numbers read in the previous steps into integers ( namely ,“123” -> 123, “0032” -> 32). If you don't read in the numbers , Then the integer is 0 . Change the symbol if necessary ( From step 2 Start ).
If the number of integers exceeds 32 Bit signed integer range [−231, 231 − 1] , You need to truncate this integer , Keep it in this range . say concretely , Less than −231 The integer of should be fixed to −231 , Greater than 231 − 1 The integer of should be fixed to 231 − 1 .
Returns an integer as the final result .
Be careful :
The white space character in this question only includes the space character ’ ’ .
Except for the leading space or the rest of the string after the number , Do not ignore Any other character .
Example 1:
Input :s = “42”
Output :42
explain : The bold string is the character that has been read in , The caret is the character currently read .
The first 1 Step :“42”( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :“42”( No characters are currently read in , Because it doesn't exist here ‘-’ perhaps ‘+’)
^
The first 3 Step :“42”( Read in “42”)
^
Parse to get an integer 42 .
because “42” In scope [-231, 231 - 1] Inside , The final result is 42 .
Example 2:
Input :s = " -42"
Output :-42
explain :
The first 1 Step :" -42"( Read in leading space , But ignore )
^
The first 2 Step :" -42"( Read in ‘-’ character , So the result should be negative )
^
The first 3 Step :" -42"( Read in “42”)
^
Parse to get an integer -42 .
because “-42” In scope [-231, 231 - 1] Inside , The final result is -42 .
Example 3:
Input :s = “4193 with words”
Output :4193
explain :
The first 1 Step :“4193 with words”( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :“4193 with words”( No characters are currently read in , Because it doesn't exist here ‘-’ perhaps ‘+’)
^
The first 3 Step :“4193 with words”( Read in “4193”; Because the next character is not a number , So read in stop )
^
Parse to get an integer 4193 .
because “4193” In scope [-231, 231 - 1] Inside , The final result is 4193 .
Tips :
0 <= s.length <= 200
s By the English letters ( Uppercase and lowercase )、 Numbers (0-9)、’ ‘、’+‘、’-’ and ‘.’ form
Their thinking :
Using regular expressions , Or use state automata algorithm .
Code implementation :
Regular method :
import re
class Solution:
def myAtoi(self, s: str) -> int:
# Regular matching
matches = re.match('[ ]*([+-]?\d+)', s)
if not matches:
return 0
# Return matching content , The contents of regular hits are 1 No. capture group
res=int(matches.group(1))
return min(max(res, -2**31), 2**31-1)
reference :
https://leetcode.cn/problems/string-to-integer-atoi/solution/xiong-mao-shua-ti-python3-yi-qi-xue-xi-zheng-ze-bi/
边栏推荐
- 187. 重复的DNA序列
- 【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
- 【快捷键】
- RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
- Take you through a circuit board, from design to production (dry goods)
- 静态库使用MFC和共享库使用MFC的区别
- 241. 为运算表达式设计优先级
- Home online shopping project
- C语言的sem_t变量类型
- pytorch训练深度学习网络设置cuda指定的GPU可见
猜你喜欢

Review column - message queue

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does

Server rendering technology JSP

MFC窗口滚动条用法
![[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理

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

4、【WebGIS实战】软件操作篇——数据导入及处理
随机推荐
6. Z 字形变换
网页不能右键 F12 查看源代码解决方案
实现pow(x,n)函数
30. 串联所有单词的子串
【EI会议】2022年第三届纳米材料与纳米技术国际会议(NanoMT 2022)
Pyramid scene parsing network [pspnet] thesis reading
Edlines: a real time line segment detector with a false detection control
Feature Pyramid Networks for Object Detection论文理解
Asgnet paper and code interpretation 2
jeecgboot输出日志,@Slf4j的使用方法
72. 编辑距离
SEM of C language_ Tvariable type
谷粒学院微信扫码登录过程记录以及bug解决
187. 重复的DNA序列
Jeecgboot output log, how to use @slf4j
Feature pyramid networks for object detection
BluePrism注册下载并安装-RPA第一章
multiple linear regression
392. 判断子序列
GCC usage, makefile summary