当前位置:网站首页>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/
边栏推荐
- Idea plug-in backup table
- Feign remote call and getaway gateway
- 409. 最长回文串
- multiple linear regression
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- How to use hybrid format to output ISO files? isohybrid:command not found
- 318. 最大单词长度乘积
- Pytorch training deep learning network settings CUDA specified GPU visible
- [reach out to Party welfare] developer reload system sequence
- Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
猜你喜欢
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Complete knapsack problem
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Jeecgboot output log, how to use @slf4j
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Addition without addition, subtraction, multiplication and division
Cookie&Session
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
4、【WebGIS实战】软件操作篇——数据导入及处理
随机推荐
8. 字符串转换整数 (atoi)
Appium自动化测试基础--补充:C/S架构和B/S架构说明
Nacos
Develop industrial Internet with the technical advantages of small programs
205. 同构字符串
idea插件备份表
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
[reach out to Party welfare] developer reload system sequence
数据库DDL(Data Definition Language,数据定义语言)知识点
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
详解Spark运行模式(local+standalone+yarn)
208. 实现 Trie (前缀树)
Use of comment keyword in database
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
Appium自动化测试基础 — APPium基本原理
Review column - message queue
30. 串联所有单词的子串
Leetcode: offer 59 - I. maximum value of sliding window
Split(), split(), slice(), can't you tell?
pytorch nn. AdaptiveAvgPool2d(1)