当前位置:网站首页>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/
边栏推荐
- Filter
- [nine day training] content III of the problem solution of leetcode question brushing Report
- 【TA-霜狼_may-《百人计划》】1.3纹理的秘密
- Edge drawing: a combined real-time edge and segment detector
- [party benefits] jsonobject to string, leave blank
- LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
- SEM of C language_ Tvariable type
- pytorch nn.AdaptiveAvgPool2d(1)
- 【TA-霜狼_may-《百人计划》】1.4 PC手机图形API介绍
- 后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
猜你喜欢
Valentine's Day is nothing.
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
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
Promql select time series
pytorch nn.AdaptiveAvgPool2d(1)
Thread data sharing and security -threadlocal
不用加减乘除实现加法
5. [WebGIS practice] software operation - service release and permission management
Avalanche problem and the use of sentinel
Online public network security case nanny level tutorial [reaching out for Party welfare]
随机推荐
ASGNet论文和代码解读2
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Thread data sharing and security -threadlocal
187. 重复的DNA序列
30. 串联所有单词的子串
Sort linked list (merge sort)
Finally in promise
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
Binary tree god level traversal: Morris traversal
Nacos
Pyramid scene parsing network [pspnet] thesis reading
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Pytorch training deep learning network settings CUDA specified GPU visible
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Detailed explanation of ES6 deconstruction grammar
The difference between MFC for static libraries and MFC for shared libraries
Complete knapsack problem
后台系统右边内容如何出现滚动条和解决双滚动条的问题
165. 比较版本号
Jeecgboot output log, how to use @slf4j