当前位置:网站首页>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/
边栏推荐
- 整合阿里云短信的问题:无法从静态上下文中引用非静态方法
- Pathmeasure implements loading animation
- Nacos
- bootsrap中的栅格系统
- 5. [WebGIS practice] software operation - service release and permission management
- Test function in pychram
- How to use hybrid format to output ISO files? isohybrid:command not found
- 详解Spark运行模式(local+standalone+yarn)
- GCC usage, makefile summary
- IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
猜你喜欢

访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

静态库使用MFC和共享库使用MFC的区别

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

谷粒学院微信扫码登录过程记录以及bug解决

Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

TEC: Knowledge Graph Embedding with Triple Context

在线公网安备案保姆级教程【伸手党福利】

Promql select time series
随机推荐
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
241. 为运算表达式设计优先级
6. Z 字形变换
AfxMessageBox和MessageBox的用法
TEC: Knowledge Graph Embedding with Triple Context
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
torch.histc
Filter
LeetCode 128最长连续序列(哈希set)
静态库使用MFC和共享库使用MFC的区别
Feature Pyramid Networks for Object Detection论文理解
Review column - message queue
392. 判断子序列
jeecgboot输出日志,@Slf4j的使用方法
409. 最长回文串
【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型
网页不能右键 F12 查看源代码解决方案
Md5sum operation
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation