topic
Atoi you to implement a function, it can convert a string to an integer.
First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.
When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.
In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.
Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.
In any case, if the function can not effectively convert, 0 is returned.
Description:
We assume that the environment can store 32-bit signed integer size, then the numerical range [-231 231--1]. If the value exceeds this range, qing return INT_MAX (231 - 1) or INT_MIN (-231).
Example 1:
Input: "42"
Output: 42
Example 2:
Input: "-42"
Output: -42
Explanation: a first non-blank character '-', it is a negative sign.
We will all digital consecutive negative number and later combined as much as possible, and finally get -42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: converting the digital OFF '3', because the next character is not numeric.
Example 4:
Input: "words and 987"
Output: 0
Explanation: a first non-blank character 'w', but it is not a positive number or negative number.
Therefore, the conversion can not be performed effectively.
Example 5:
Input: "-91283472332"
Output: -2147483648
explanation: the number "-91283472332" Over the range of 32-bit signed integer.
So return INT_MIN (-231).
Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/string-to-integer-atoi
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Thinking
Seventh title with the same method of cross-border judgment. Digital split into 'highest to ten' × 10 + bits so that each determination 'MSB - ten' is greater than, equal in front of bits is greater than, determines whether the overflow line, a negative becomes positive number.
Make it clear we may encounter
- Remove the spaces in front of the string
- First encountered non-space is not a number '-' + 'output 0
- First encountered a number, it can only deal with continuous digital
- First encountered symbol, behind only handle digital
distinguish these four cases will be happy to write code.
Code
class Solution:
def myAtoi(self, str: str) -> int:
maxa=2**31-1
maxb=2**31
flag=0
f=0
ans=0
for i in str:
if (i>='0' and i<='9') or i=='-' or i=='+':
if f==0 and i!='-' and i!='+':
flag=1
f=1
if i=='-':
if flag!=0:
return ans*flag
else:
flag=-1
elif i=='+':
if flag!=0:
return ans*flag
else:
flag=1
elif flag==0 or flag==1:
s=int(i)
if ans>maxa//10:
return maxa
elif ans==maxa//10 and s>7:
return maxa
else:
ans=ans*10+s
else:
s=int(i)
if ans>maxb//10:
return maxb*flag
elif ans==maxb//10 and s>8:
return maxb*flag
else:
ans=ans*10+s
elif i==' ' and f==0:
continue
else:
if flag==0:
flag=1
return ans*flag
if flag==0:
flag=1
return ans*flag